-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions-color.php
138 lines (129 loc) · 3.72 KB
/
functions-color.php
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Returns an array of luminance variants of $base_color
*
* @since 0.1.0
* @access public
* @return void
*/
function an_colorsplosion($base_color) {
$RGB = array(
"R" => hexdec(substr($base_color, 1, 2)),
"G" => hexdec(substr($base_color, 3, 2)),
"B" => hexdec(substr($base_color, 5, 2))
);
$color_array = array();
$luminance = an_luminance($base_color);
for($i=1; $i<=100; $i++){
$color_array['darker'][$i] = "#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, ($i/100))));
$color_array['lighter'][$i] = "#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, (-($i/100)))));
if($luminance > 50) {
$color_array['high_contrast'][$i] = "#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, (($i+(100-$luminance))/(100+(100-$luminance))))));
$color_array['low_contrast'][$i] = "#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, (-(($i+(100-$luminance))/(100+(100-$luminance)))))));
} else {
$color_array['low_contrast'][$i] = "#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, (($i+$luminance)/(100+$luminance)))));
$color_array['high_contrast'][$i] = "#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, (-(($i+$luminance)/(100+$luminance))))));
}
}
return $color_array;
}
/**
* Shifts the luminance of a single color by $shift in the direction $direction
*
* @since 0.1.0
* @access public
* @return void
*/
function shift_luminance($color, $shift, $direction="high_contrast") {
$RGB = array(
"R" => hexdec(substr($color, 1, 2)),
"G" => hexdec(substr($color, 3, 2)),
"B" => hexdec(substr($color, 5, 2))
);
$luminance = an_luminance($color);
switch($direction){
case "lighter":
return ("#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, -($shift/100)))));
break;
case "darker":
return ("#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, ($shift/100)))));
break;
case "high_contrast":
if($luminance < 50){
return ("#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, -($shift/100)))));
} else {
return ("#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, ($shift/100)))));
}
break;
case "low_contrast":
if($luminance < 50){
return ("#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, ($shift/100)))));
} else {
return ("#" . an_array2color(an_dec2hex(an_shift_luminance($RGB, -($shift/100)))));
}
break;
}
}
/**
* Calculates luminance of a color.
*
* @since 0.1.0
* @access public
* @return void
*/
function an_luminance($color) {
$RGB = array(
"R" => hexdec(substr($color, 1, 2)),
"G" => hexdec(substr($color, 3, 2)),
"B" => hexdec(substr($color, 5, 2))
);
$luminance = ((.299 * $RGB["R"]) + (.587 * $RGB["G"]) + (.114 * $RGB["B"])) / 2.55;
return $luminance;
}
/**
* Shifts the luminance of a color towards black or white by the given factor.
*
* @since 0.1.0
* @access public
* @return void
*/
function an_shift_luminance($RGB, $shift){
if($shift < 0){
foreach($RGB as $key => $val){
$RGB[$key] = round($val - ((255 - $val) * $shift));
}
return $RGB;
} else {
foreach($RGB as $key => $val){
$RGB[$key] = round($val * (1 - $shift));
}
return $RGB;
}
}
/**
* Converts a decimal RGB array to a hex array.
*
* @since 0.1.0
* @access public
* @return void
*/
function an_dec2hex($RGB){
foreach($RGB as $key => $val){
$RGB[$key] = dechex($RGB[$key]);
if(strlen($RGB[$key])==1){
$RGB[$key] = '0' . $RGB[$key];
}
}
return $RGB;
}
/**
* Converts a hex RGB array to a web color.
*
* @since 0.1.0
* @access public
* @return void
*/
function an_array2color ($RGB) {
return ($RGB['R'] . $RGB['G'] . $RGB['B']);
}
?>