-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette.php
193 lines (160 loc) · 4.79 KB
/
palette.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
<?php
namespace Chirp;
// Original PHP code by Chirp Internet: www.chirpinternet.eu
// Please acknowledge use of this code by including this header.
class ImageSampler
{
private $img;
private $callback = NULL;
private $initialized = FALSE;
protected $percent = 5;
protected $steps = 10;
public $w, $h;
public $sample_w = 0;
public $sample_h = 0;
public function __construct($imagefile)
{
if(!$this->img = imagecreatefromjpeg($imagefile)) {
die("Error loading image: {$imagefile}");
}
$this->w = imagesx($this->img);
$this->h = imagesy($this->img);
}
public function set_percent($percent)
{
$percent = intval($percent);
if(($percent < 1) || ($percent > 50)) {
die("Your \$percent value needs to be between 1 and 50.");
}
$this->percent = $percent;
}
public function set_steps($steps)
{
$steps = intval($steps);
if(($steps < 1) || ($steps > 50)) {
die("Your \$steps value needs to be between 1 and 50.");
}
$this->steps = $steps;
}
private function set_callback($callback)
{
try {
$fn = new \ReflectionFunction($callback);
if($fn->getNumberOfParameters() != 5) {
throw new \ReflectionException("Invalid parameter count in callback function. Usage: fn(int, int, int, bool) { ... }");
}
$this->callback = $callback;
} catch(\ReflectionException $e) {
die($e->getMessage());
}
}
public function init()
{
$this->sample_w = $this->w / $this->steps;
$this->sample_h = $this->h / $this->steps;
$this->initialized = TRUE;
}
private function get_pixel_color($x, $y)
{
$rgb = imagecolorat($this->img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
return [$r, $g, $b];
}
public function sample($callback = NULL)
{
if(!$this->initialized) {
$this->init();
}
if(($this->sample_w < 2) || ($this->sample_h < 2)) {
die("Your sampling size is too small for this image - reduce the \$steps value.");
}
if($callback) {
$this->set_callback($callback);
}
$sample_size = round($this->sample_w * $this->sample_h * $this->percent / 100);
$retval = [];
for($i=0, $y=0; $i < $this->steps; $i++, $y += $this->sample_h) {
$flag = FALSE;
$row_retval = [];
for($j=0, $x=0; $j < $this->steps; $j++, $x += $this->sample_w) {
$total_r = $total_g = $total_b = 0;
for($k=0; $k < $sample_size; $k++) {
$pixel_x = $x + rand(0, $this->sample_w-1);
$pixel_y = $y + rand(0, $this->sample_h-1);
list($r, $g, $b) = $this->get_pixel_color($pixel_x, $pixel_y);
$total_r += $r;
$total_g += $g;
$total_b += $b;
}
$avg_r = round($total_r/$sample_size);
$avg_g = round($total_g/$sample_size);
$avg_b = round($total_b/$sample_size);
if($this->callback) {
call_user_func_array($this->callback, [$avg_r, $avg_g, $avg_b, $w, !$flag]);
}
$row_retval[] = [$avg_r, $avg_g, $avg_b];
$w = $pixel_x;
$flag = TRUE;
}
$retval[] = $row_retval;
}
return $retval;
}
}
$files = glob('tmp/*');
$img = $files[0];
$sampler = new \Chirp\ImageSampler($img);
$sampler->set_percent(15);
$sampler->set_steps(7);
$sampler->init();
$colors = array();
$sampler_callback = function($r, $g, $b, $w, $new_row) {
global $colors;
$color = $r . "," . $g . "," . $b;
$entry = array(
'w' => $w,
'color' => $color
);
array_push($colors, $entry);
};
$sampler->sample($sampler_callback);
function getBright($palette, $brightness_limit=400) {
global $max;
global $diff_limit;
$bright_colors = array();
$filtered_bright_colors = array();
$existing_sums = array();
$ci = 0;
foreach($palette as $i => $color) {
$xcolor = explode(",", $color['color']);
$sum = array_sum($xcolor);
if(!in_array($sum, $existing_sums)) array_push($existing_sums, $sum);
}
$average = array_sum($existing_sums)/count($existing_sums);
$brightness_min = $average * .1;
$brightness_max = $average * .9;
foreach($palette as $i => $color) {
$xcolor = explode(",", $color['color']);
$sum = array_sum($xcolor);
if(!in_array($sum, $existing_sums)) array_push($existing_sums, $sum);
if($sum >= $brightness_min && $sum <= $brightness_max) {
foreach($existing_sums as $existing_sum){
if( $existing_sum > $sum+$comp && $exiting_sum < $sum-$comp && !in_array($color, $bright_colors)){
$color_entry = array(
'w' => $color['w'],
'color' => $color['color']
);
array_push($bright_colors, $color_entry);
$ci++;
}
}
}
}
$final_list = $bright_colors;
sort($final_list);
return $final_list;
}
echo json_encode( getBright($colors, 60) );
?>