-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorizer.php
221 lines (164 loc) · 4.36 KB
/
colorizer.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Colorizer Class
*
* @package Colorizer
* @author ANEX
* @version 1.0.0
*/
class Colorizer {
/**
* Get Color
* Returns the final color
*
* @param string $color pass a rgb color code
* @param array $modifier pass additional attributes
*
* @since 1.0.0
*/
public static function get_color( $color = null, $modifier = array() ) {
// Abort if no color provided
if( empty( $color ) )
return;
if( !array_key_exists( 'type', $modifier ) )
$modifier['type'] = null;
if( !array_key_exists( 'change', $modifier ) )
$modifier['change'] = null;
if( !array_key_exists( 'opacity', $modifier ) )
$modifier['opacity'] = null;
return self::rgb2rgba( self::rgb2str( self::color_shade( self::str2rgb( $color ), $modifier['type'], $modifier['change'] ) ), $modifier['opacity'] );
}
/**
* RGB to RGBA
* Convert RGB color value to RGBA color value
*
* @param string $color
* @param string $opacity
*
* @since 1.0.0
*/
public static function rgb2rgba( $color, $opacity = false ) {
// Abort if no color provided
if( empty( $color ) )
return;
// Sanitize $color if "rgba()" is provided
$color = substr( $color, 4, -1 );
// Check if opacity is set(rgba or rgb)
if( $opacity ) {
if( abs( $opacity ) > 1)
$opacity = 1.0;
$output = 'rgba(' . $color . ',' . $opacity . ')';
} else {
$output = 'rgb(' . $color . ')';
}
// Return rgb(a) color string
return $output;
}
/**
* HEX to RGB(A)
* Convert RGB color value to RGB(A) color value
*
* @param string $color
* @param string $opacity
*
* @since 1.1.0
*/
public static function hex2rgba( $color, $opacity = false ) {
$default = 'rgb(0,0,0)';
//Return default if no color provided
if( empty( $color ) )
return $default;
//Sanitize $color if "#" is provided
if ( $color[0] == '#' ) {
$color = substr( $color, 1 );
}
//Check if color has 6 or 3 characters and get values
if ( strlen( $color ) == 6 ) {
$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
} elseif ( strlen( $color ) == 3 ) {
$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
} else {
return $default;
}
//Convert hexadec to rgb
$rgb = array_map( 'hexdec', $hex );
//Check if opacity is set(rgba or rgb)
if( $opacity ){
if( abs( $opacity ) > 1 )
$opacity = 1.0;
$output = 'rgba('.implode(",",$rgb).','.$opacity.')';
} else {
$output = 'rgb('.implode(",",$rgb).')';
}
//Return rgb(a) color string
return $output;
}
/**
* Color Shade
* Calculates color shades of a given color
*
* @param array $rgb - define a rgb color code
* @param string $type - choose lighter or leave empty
* @param int $change - defines the change of the base color (eg. 255 -> 250)
*
* @since 1.0.0
*/
public static function color_shade( array $rgb, $type, $change = 5 ) {
if( $type == 'lighter' ) {
$rgb[0] = 255-( 255-$rgb[0] ) + $change;
$rgb[1] = 255-( 255-$rgb[1] ) + $change;
$rgb[2] = 255-( 255-$rgb[2] ) + $change;
} else {
$rgb[0] -= $change;
$rgb[1] -= $change;
$rgb[2] -= $change;
}
return $rgb;
}
/**
* String to RGB
* Converts a given string to a RGB Color Value
*
* @param array $str
*
* @since 1.0.0
*/
public static function str2rgb( $str ) {
if( is_array( $str ) )
return $str;
$str = preg_replace( '/\s+/', '', $str ); // replace all spaces
$str = str_replace( array( 'rgba(', 'rgb(', ')' ), '', $str );
$comp = explode( ',', $str, 4 );
$cnt = count( $comp );
if( $cnt < 3 || $cnt > 4 )
return array( 0,0,0 );
return array_map( 'floatval', $comp );
}
/**
* RGB to String
* Converts a given RGB Color Value to a String
*
* @param array $rgb
* @param bool $raw
*
* @since 1.0.0
*/
public static function rgb2str( $rgb, $raw = false ) {
$str = implode( ',', $rgb );
if( $raw )
return $str;
return ( ( count( $rgb ) == 3 ) ? 'rgb(' : 'rgba(' ) . $str . ')';
}
}
/**
* Colorizer Function
* Base function which can be used for color manipulation
*
* @param string $color
* @param array $modifier
*
* @since 1.0.0
*/
function colorizer( $color = null, $modifier = array() ) {
return Colorizer::get_color( $color, $modifier );
}