Skip to content

Commit a6d5cc8

Browse files
committed
Merge pull request #6 from funcorp/fix-hsv-convertation
Fix HSV to RGB, CMYK to RBG
2 parents 3642661 + 9b72e72 commit a6d5cc8

File tree

12 files changed

+297
-59
lines changed

12 files changed

+297
-59
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/tests export-ignore
2+
.gitattributes export-ignore
3+
.gitignore export-ignore
4+
phpunit.xml export-ignore

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.lock
3+
.DS_Store
4+
Thumbs.db

composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@
1414
"require": {
1515
"php": ">=5.3.3"
1616
},
17+
"require-dev": {
18+
"phpunit/phpunit": "~4.0"
19+
},
1720
"type": "library",
1821
"autoload": {
1922
"psr-0": {
2023
"Primal\\Color": "lib\/"
2124
}
2225
},
26+
"autoload-dev": {
27+
"psr-0": {
28+
"Tests\\Primal\\Color\\": "tests/unit"
29+
}
30+
},
2331
"extra": {
2432
"branch-alias": {
2533
"dev-master": "1.0.x-dev"

lib/Primal/Color/CMYKColor.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33
namespace Primal\Color;
44

@@ -8,45 +8,46 @@ class CMYKColor extends Color {
88
public $yellow = 0;
99
public $black = 100;
1010
public $alpha = 1;
11-
11+
1212
function __construct($c = 0, $m = 0, $y = 0, $k = 100, $a=1) {
1313
$this->cyan = $c;
1414
$this->magenta = $m;
1515
$this->yellow = $y;
1616
$this->black = $k;
1717
$this->alpha = $a;
1818
}
19-
19+
2020
function toHSV() {
2121
return $this->toRGB()->toHSV();
2222
}
23-
23+
2424
function toHSL() {
2525
return $this->toRGB()->toHSL();
2626
}
27-
27+
2828
function toCMYK() {
2929
return clone $this;
3030
}
31-
31+
3232
function toRGB() {
3333
$c = ((int)$this->cyan % 100) / 100;
3434
$m = ((int)$this->magenta % 100) / 100;
3535
$y = ((int)$this->yellow % 100) / 100;
36-
$b = ((int)$this->black % 100) / 100;
37-
$a = $this->alpha;
38-
36+
$k = ((int)$this->black % 100) / 100;
37+
3938
$r = 1 - min(1, $c * (1 - $k) + $k);
4039
$g = 1 - min(1, $m * (1 - $k) + $k);
4140
$b = 1 - min(1, $y * (1 - $k) + $k);
42-
43-
return new RGBColor($r * 255, $g * 255, $b * 255);
44-
}
45-
46-
function toCSS() {
47-
return "device-cmyk({$this->cyan}, {$this->magenta}, {$this->yellow}, {$this->black})";
41+
42+
return new RGBColor($r * 255, $g * 255, $b * 255, $this->alpha);
4843
}
4944

50-
45+
function toCSS($alpha = null) {
46+
return ($alpha === true || $this->alpha < 1) && $alpha !== false
47+
? sprintf('device-cmyk(%d%%, %d%%, %d%%, %d%%, %s)',
48+
$this->cyan, $this->magenta, $this->yellow, $this->black, $this->alpha)
49+
: sprintf('device-cmyk(%d%%, %d%%, %d%%, %d%%)',
50+
$this->cyan, $this->magenta, $this->yellow, $this->black);
51+
}
5152
}
5253

lib/Primal/Color/HSLColor.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33
namespace Primal\Color;
44

@@ -7,45 +7,45 @@ class HSLColor extends Color {
77
public $saturation = 0;
88
public $luminance = 0;
99
public $alpha = 1;
10-
10+
1111
function __construct($h = 0, $s = 0, $l = 0, $a=1) {
1212
$this->hue = $h;
1313
$this->saturation = $s;
1414
$this->luminance = $l;
1515
$this->alpha = $a;
1616
}
17-
17+
1818
public function toHSV() {
1919
return $this->toRGB()->toHSV();
2020
}
21-
21+
2222
public function toHSL() {
2323
return clone $this;
2424
}
25-
25+
2626
function toCMYK() {
2727
return $this->toRGB()->toCMYK();
2828
}
29-
29+
3030
public function toRGB() {
3131
$h = ($this->hue % 360) / 360;
3232
$s = ($this->saturation % 101) / 100;
3333
$l = ($this->luminance % 101) / 100;
34-
$a = $this->alpha;
35-
34+
3635
if ($s === 0) {
37-
return new RGBColor($l * 255, $l * 255, $l * 255);
36+
return new RGBColor($l * 255, $l * 255, $l * 255, $this->alpha);
3837
} else {
39-
$q = ($l < 0.5) ? $l * (1 + $s) : $l + $s - $l * $s;
38+
$q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s;
4039
$p = 2 * $l - $q;
4140
return new RGBColor(
4241
static::HueToFactor($p, $q, $h + 1 / 3) * 255,
4342
static::HueToFactor($p, $q, $h ) * 255,
44-
static::HueToFactor($p, $q, $h - 1 / 3) * 255
43+
static::HueToFactor($p, $q, $h - 1 / 3) * 255,
44+
$this->alpha
4545
);
4646
}
4747
}
48-
48+
4949
protected static function HueToFactor($p, $q, $t) {
5050
if ($t < 0) $t += 1;
5151
if ($t > 1) $t -= 1;
@@ -54,9 +54,11 @@ protected static function HueToFactor($p, $q, $t) {
5454
if ($t < 2 / 3) return $p + ($q - $p) * (2/3 - $t) * 6;
5555
return $p;
5656
}
57-
57+
5858
public function toCSS($alpha = null) {
59-
return (($alpha === true || $this->alpha < 1) && $alpha !== false) ? "hsla({$this->hue}, {$this->saturation}, {$this->luminance}, {$this->alpha})" : "hsl({$this->hue}, {$this->saturation}, {$this->luminance})";
59+
return ($alpha === true || $this->alpha < 1) && $alpha !== false
60+
? sprintf('hsla(%d, %d, %d, %s)', $this->hue, $this->saturation, $this->luminance, $this->alpha)
61+
: sprintf('hsl(%d, %d, %d)', $this->hue, $this->saturation, $this->luminance);
6062
}
6163
}
6264

lib/Primal/Color/HSVColor.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33
namespace Primal\Color;
44

@@ -7,34 +7,34 @@ class HSVColor extends Color {
77
public $saturation = 0;
88
public $value = 0;
99
public $alpha = 1;
10-
10+
1111
function __construct($h = 0, $s = 0, $v = 0, $a=1) {
1212
$this->hue = $h;
1313
$this->saturation = $s;
1414
$this->value = $v;
1515
$this->alpha = $a;
1616
}
17-
17+
1818
function toHSV() {
1919
return clone $this;
2020
}
21-
21+
2222
function toHSL() {
2323
return $this->toRGB()->toHSL();
2424
}
25-
25+
2626
function toCMYK() {
2727
return $this->toRGB()->toCMYK();
2828
}
29-
29+
3030
function toRGB() {
3131
$h = ($this->hue % 360) / 360;
3232
$s = ($this->saturation % 101) / 100;
3333
$v = ($this->value % 101) / 100;
3434
$a = $this->alpha;
35-
35+
3636
$i = $h * 6;
37-
$f = $h * 6 - $i;
37+
$f = $i - floor($i);
3838
$p = $v * (1 - $s);
3939
$q = $v * (1 - $f * $s);
4040
$t = $v * (1 - (1 - $f) * $s);
@@ -72,10 +72,10 @@ function toRGB() {
7272
}
7373
return new RGBColor($r * 255, $g * 255, $b * 255, $a);
7474
}
75-
75+
7676
function toCSS($alpha = null) {
77-
return (($alpha === true || $this->alpha < 1) && $alpha !== false) ? "hsla({$this->hue}, {$this->saturation}, {$this->value}, {$this->alpha})" : "hsl({$this->hue}, {$this->saturation}, {$this->value})";
77+
return $this->toRGB()->toCSS($alpha);
7878
}
79-
79+
8080
}
8181

lib/Primal/Color/RGBColor.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33
namespace Primal\Color;
44

@@ -7,28 +7,28 @@ class RGBColor extends Color {
77
public $green = 0;
88
public $blue = 0;
99
public $alpha = 1;
10-
10+
1111
function __construct($r = 0, $g = 0, $b = 0, $a=1) {
1212
$this->red = $r;
1313
$this->green = $g;
1414
$this->blue = $b;
1515
$this->alpha = $a;
1616
}
17-
17+
1818
function toHSV() {
1919
$r = ((int)$this->red % 256) / 255;
2020
$g = ((int)$this->green % 256) / 255;
2121
$b = ((int)$this->blue % 256) / 255;
2222
$a = $this->alpha;
23-
23+
2424
$max = max($r, $g, $b);
2525
$min = min($r, $g, $b);
2626
$d = $max - $min;
2727

2828
$h = 0;
2929
$s = ($max === 0) ? 0 : $d / $max;
3030
$v = $max;
31-
31+
3232
if ($max !== $min) {
3333
switch ($max) {
3434
case $r:
@@ -42,19 +42,19 @@ function toHSV() {
4242
}
4343
$h = $h / 6;
4444
}
45-
45+
4646
return new HSVColor($h*360, $s*100, $v*100, $a);
4747
}
48-
48+
4949
function toHSL() {
5050
$r = ((int)$this->red % 256) / 255;
5151
$g = ((int)$this->green % 256) / 255;
5252
$b = ((int)$this->blue % 256) / 255;
5353
$a = $this->alpha;
54-
54+
5555
$max = max($r, $g, $b);
5656
$min = min($r, $g, $b);
57-
57+
5858
$l = ($max + $min ) / 2;
5959
if ($max === $min) {
6060
$h = $s = 0;
@@ -73,10 +73,10 @@ function toHSL() {
7373
}
7474
$h = $h / 6;
7575
}
76-
76+
7777
return new HSLColor($h*360, $s*100, $l*100, $a);
7878
}
79-
79+
8080
function toCMYK() {
8181
$r = ((int)$this->red % 256) / 255;
8282
$g = ((int)$this->green % 256) / 255;
@@ -91,26 +91,28 @@ function toCMYK() {
9191
$c = (1 - $r - $k) / (1 - $k);
9292
$m = (1 - $g - $k) / (1 - $k);
9393
$y = (1 - $b - $k) / (1 - $k);
94-
94+
9595
return new CMYKColor($c*100, $m*100, $y*100, $k*100, $a);
9696
}
97-
97+
9898
function toRGB() {
9999
return clone $this;
100100
}
101-
101+
102102
function toCSS($alpha = null) {
103-
return (($alpha === true || $this->alpha < 1) && $alpha !== false) ? "rgba({$this->red}, {$this->green}, {$this->blue}, {$this->alpha})" : "rgb({$this->red}, {$this->green}, {$this->blue})";
103+
return ($alpha === true || $this->alpha < 1) && $alpha !== false
104+
? sprintf('rgba(%d, %d, %d, %s)', $this->red, $this->green, $this->blue, $this->alpha)
105+
: sprintf('rgb(%d, %d, %d)', $this->red, $this->green, $this->blue);
104106
}
105-
107+
106108
function toHex() {
107109
$stack = array('#');
108110
$stack[] = str_pad(dechex(min(255, round($this->red ))), 2, '0', STR_PAD_LEFT);
109111
$stack[] = str_pad(dechex(min(255, round($this->green))), 2, '0', STR_PAD_LEFT);
110112
$stack[] = str_pad(dechex(min(255, round($this->blue ))), 2, '0', STR_PAD_LEFT);
111-
113+
112114
return implode('', $stack);
113115
}
114-
116+
115117
}
116118

phpunit.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false"
8+
syntaxCheck="false">
9+
<testsuites>
10+
<testsuite name="Unit tests">
11+
<directory>./tests/unit/</directory>
12+
</testsuite>
13+
</testsuites>
14+
</phpunit>

0 commit comments

Comments
 (0)