Completed
Push — master ( 68735c...ea470f )
by Björn
11:25
created

Util::invertTextColor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
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
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Theming;
23
24
class Util {
25
26
	/**
27
	 * @param string $color rgb color value
28
	 * @return bool
29
	 */
30
	public static function invertTextColor($color) {
31
		$l = self::calculateLuminance($color);
32
		if($l>0.5) {
33
			return true;
34
		} else {
35
			return false;
36
		}
37
	}
38
39
	/**
40
	 * @param string $color rgb color value
41
	 * @return float
42
	 */
43
	public static function calculateLuminance($color) {
44
		$hex = preg_replace("/[^0-9A-Fa-f]/", '', $color);
45
		if (strlen($hex) === 3) {
46
			$hex = $hex{0} . $hex{0} . $hex{1} . $hex{1} . $hex{2} . $hex{2};
47
		}
48
		if (strlen($hex) !== 6) {
49
			return 0;
50
		}
51
		$r = hexdec(substr($hex, 0, 2));
52
		$g = hexdec(substr($hex, 2, 2));
53
		$b = hexdec(substr($hex, 4, 2));
54
		return (0.299 * $r + 0.587 * $g + 0.114 * $b)/255;
55
	}
56
57
}
58