class-fl-builder-color.php
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* Helper class for working with color values.
*
* @since 1.0
*/
final class FLBuilderColor {
/**
* Converts a hex string into an array of RGB values.
*
* @since 1.0
* @param string $hex A hex color value without the # sign.
* @return array An array of RGB values.
*/
static public function hex_to_rgb($hex)
{
return array(
'r' => hexdec(substr($hex,0,2)),
'g' => hexdec(substr($hex,2,2)),
'b' => hexdec(substr($hex,4,2))
);
}
/**
* Adjusts the brightness of a hex color value based on
* the number of steps provided.
*
* @since 1.0
* @param string $hex A hex color value without the # sign.
* @param int $steps The number of steps to adjust the color.
* @param string $type The type of adjustment to make. Either lighten, darken or reverse.
* @return string The adjusted hex string.
*/
static public function adjust_brightness($hex, $steps, $type)
{
// Get rgb vars.
extract(self::hex_to_rgb($hex));
// Should we darken the color?
if($type == 'reverse' && $r + $g + $b > 382){
$steps = -$steps;
}
else if($type == 'darken') {
$steps = -$steps;
}
// Build the new color.
$steps = max(-255, min(255, $steps));
$r = max(0,min(255,$r + $steps));
$g = max(0,min(255,$g + $steps));
$b = max(0,min(255,$b + $steps));
$r_hex = str_pad(dechex($r), 2, '0', STR_PAD_LEFT);
$g_hex = str_pad(dechex($g), 2, '0', STR_PAD_LEFT);
$b_hex = str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
return $r_hex . $g_hex . $b_hex;
}
}