-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPKRoundColor.php
More file actions
97 lines (81 loc) · 2.88 KB
/
PKRoundColor.php
File metadata and controls
97 lines (81 loc) · 2.88 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
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
<?php
/**
* Projekod Interactive
* http://projekod.com
*/
class PKRoundColor {
protected $baseColors = array(
"964b00", //Brown
"000000", //Black
"403d3f", //smoked (Turkish : Füme )
"808080", //Gray
"f5f5dc", //Beige (Turkish : Bej )
"ffffff", //White
"ffcbdb", //Pink
"ff00ff", //Fuchsia (Turkish : Fuşya )
"9370db", //Purple
"b03060", //Claret Red (Turkish : Bize her yer trabzon! )
"0000ff", //Blue
"40e0d0", //Turquoise
"00ff00", //Green
"ffff00", //Yellow
"ff7f00", //Orange
"ff0000" //Red
);
public function getBaseColors(){
return $this->baseColors;
}
public function addBaseColor($color){
if($this->checkHexColor($color)){
$this->baseColors[] = $color;
array_unique($this->baseColors);
}
}
public function getRoundedColor($color){
if($this->checkHexColor($color)){
$rgbColor = $this->hex2rgb($color,true);
$part = explode(",",$rgbColor);
$redColor = $part[0];
$greenColor = $part[1];
$blueColor = $part[2];
$distinction = array();
$index = 0;
foreach($this->baseColors as $baseColor){
$baseColor = strtoupper($baseColor);
$baseRgbColor = $this->hex2rgb($baseColor,true);
$basePart = explode(",",$baseRgbColor);
$baseRedColor = $basePart[0];
$baseGreenColor = $basePart[1];
$baseBlueColor = $basePart[2];
$sqrt =sqrt(($redColor-$baseRedColor)*($redColor-$baseRedColor) +
($greenColor-$baseGreenColor) * ($greenColor - $baseGreenColor) +
($blueColor-$baseBlueColor) * ($blueColor - $baseBlueColor));
$distinction["$sqrt"] = $index;
$index++;
}
$minValue = min(array_keys($distinction));
$index = $distinction[$minValue];
return '#'.$this->baseColors[$index];
}
}
public function checkHexColor($color){
return preg_match('/^#[a-f0-9]{6}$/i', $color);
}
public function hex2rgb($hexVal,$implode=false) {
$hexVal = str_replace("#", "", $hexVal);
if(strlen($hexVal) == 3) { //Like #000
$red = hexdec(substr($hexVal,0,1).substr($hexVal,0,1));
$green = hexdec(substr($hexVal,1,1).substr($hexVal,1,1));
$blue = hexdec(substr($hexVal,2,1).substr($hexVal,2,1));
} else {
$red = hexdec(substr($hexVal,0,2));
$green = hexdec(substr($hexVal,2,2));
$blue = hexdec(substr($hexVal,4,2));
}
$rgb = array($red, $green, $blue);
if($implode){
return implode(",", $rgb);
}
return $rgb;
}
}