Completed
Push — master ( 733110...9ebd09 )
by Jan-Christoph
36:21 queued 22:29
created

Util::invertTextColor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Haertl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Theming;
25
26
class Util {
27
28
	/**
29
	 * @param string $color rgb color value
30
	 * @return bool
31
	 */
32
	public static function invertTextColor($color) {
33
		$l = self::calculateLuminance($color);
34
		if($l>0.5) {
35
			return true;
36
		} else {
37
			return false;
38
		}
39
	}
40
41
	/**
42
	 * get color for on-page elements:
43
	 * theme color by default, grey if theme color is to bright
44
	 * @param $color
45
	 * @return string
46
	 */
47
	public static function elementColor($color) {
48
		$l = self::calculateLuminance($color);
49
		if($l>0.8) {
50
			return '#555555';
51
		} else {
52
			return $color;
53
		}
54
	}
55
56
	/**
57
	 * @param string $color rgb color value
58
	 * @return float
59
	 */
60
	public static function calculateLuminance($color) {
61
		$hex = preg_replace("/[^0-9A-Fa-f]/", '', $color);
62
		if (strlen($hex) === 3) {
63
			$hex = $hex{0} . $hex{0} . $hex{1} . $hex{1} . $hex{2} . $hex{2};
64
		}
65
		if (strlen($hex) !== 6) {
66
			return 0;
67
		}
68
		$r = hexdec(substr($hex, 0, 2));
69
		$g = hexdec(substr($hex, 2, 2));
70
		$b = hexdec(substr($hex, 4, 2));
71
		return (0.299 * $r + 0.587 * $g + 0.114 * $b)/255;
72
	}
73
74
	/**
75
	 * @param $color
76
	 * @return string base64 encoded radio button svg
77
	 */
78
	public static function generateRadioButton($color) {
79
		$radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
80
			'<path d="M8 1a7 7 0 0 0-7 7 7 7 0 0 0 7 7 7 7 0 0 0 7-7 7 7 0 0 0-7-7zm0 1a6 6 0 0 1 6 6 6 6 0 0 1-6 6 6 6 0 0 1-6-6 6 6 0 0 1 6-6zm0 2a4 4 0 1 0 0 8 4 4 0 0 0 0-8z" fill="'.$color.'"/></svg>';
81
		return base64_encode($radioButtonIcon);
82
	}
83
84
}
85