-
Notifications
You must be signed in to change notification settings - Fork 12
/
tonesque.php
164 lines (143 loc) · 3.51 KB
/
tonesque.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
<?php
/*
Plugin Name: Tonesque
Plugin URI: https://github.com/mtias/tonesque
Description: Class to grab an average color representation from an image.
Version: 1.0
Author: Matias Ventura
Author URI: http://matiasventura.com
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
class Tonesque {
private $image = '';
private $color = '';
function __construct( $image ) {
if ( ! class_exists( 'Color' ) )
require_once __DIR__ . '/class.color.php';
$this->image = esc_url_raw( $image );
}
/**
*
* Construct object from image.
*
* @param optional $type (hex, rgb, hsl)
* @return color as a string formatted as $type
*
*/
function color( $points = 5, $type = 'hex' ) {
// Bail if there is no image to work with
if ( ! $this->image )
return false;
$image = trim( $this->image );
// Grab the extension
$file = strtolower( pathinfo( $image, PATHINFO_EXTENSION ) );
$file = explode( '?', $file );
$file = $file[ 0 ];
switch ( $file ) {
case 'gif' :
$img = imagecreatefromgif( $image );
break;
case 'png' :
$img = imagecreatefrompng( $image );
break;
case 'jpg' :
case 'jpeg' :
$img = imagecreatefromjpeg( $image );
break;
default:
return false;
}
// Finds dominant color
$color = self::grab_color( $img, $points );
// Passes value to Color class
$color = self::get_color( $color, $type );
return $color;
}
/**
*
* Finds the average color of the image based on five sample points
*
* @param $image
* @return array() with rgb color
*
*/
function grab_color( $image, $points ) {
$img = $image;
$height = imagesy( $img );
$width = imagesx( $img );
// Sample five points in the image
// Based on rule of thirds and center
$topy = round( $height / 3 );
$bottomy = round( ( $height / 3 ) * 2 );
$leftx = round( $width / 3 );
$rightx = round( ( $width / 3 ) * 2 );
$centery = round( $height / 2 );
$centerx = round( $width / 2 );
// Cast those colors into an array
$rgb = array(
imagecolorat( $img, $leftx, $topy ),
imagecolorat( $img, $rightx, $topy ),
imagecolorat( $img, $leftx, $bottomy ),
imagecolorat( $img, $rightx, $bottomy ),
imagecolorat( $img, $centerx, $centery ),
);
// todo: use $points
// Process the color points
// Find the average representation
foreach ( $rgb as $color ) {
$index = imagecolorsforindex( $img, $color );
$r[] = $index['red'];
$g[] = $index['green'];
$b[] = $index['blue'];
$red = round( array_sum( $r ) / $points );
$green = round( array_sum( $g ) / $points );
$blue = round( array_sum( $b ) / $points );
}
// The average color of the image as rgb array
$color = array(
'r' => $red,
'g' => $green,
'b' => $blue,
);
return $color;
}
/**
*
* Get a Color object using /lib class.color
* Convert to appropiatte type
*
* @return string
*
*/
function get_color( $color, $type ) {
$c = new Color( $color, 'rgb' );
$this->color = $c;
switch ( $type ) {
case 'rgb' :
$color = implode( $c->toRgbInt(), ',' );
break;
case 'hex' :
$color = $c->toHex();
break;
case 'hsv' :
$color = implode( $c->toHsvInt(), ',' );
break;
default:
return $color = $c->toHex();
}
return $color;
}
/**
*
* Checks contrast against main color
* Gives either black or white for using with opacity
*
* @return string
*
*/
function contrast() {
$c = $this->color->getMaxContrastColor();
return implode( $c->toRgbInt(), ',' );
}
};