From a4ac1b85922c7c19a2ca19a1451188e426399108 Mon Sep 17 00:00:00 2001 From: Ilyas Salikhov Date: Thu, 31 Jul 2014 13:04:06 +0400 Subject: [PATCH] First version + composer.json + README --- ColorInterpolator/Color.php | 133 ++++++++++++++++++++++++ ColorInterpolator/ColorInterpolator.php | 43 ++++++++ README.md | 19 +++- composer.json | 19 ++++ 4 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 ColorInterpolator/Color.php create mode 100644 ColorInterpolator/ColorInterpolator.php create mode 100644 composer.json diff --git a/ColorInterpolator/Color.php b/ColorInterpolator/Color.php new file mode 100644 index 0000000..6e276e5 --- /dev/null +++ b/ColorInterpolator/Color.php @@ -0,0 +1,133 @@ +setRgb($rgb); + } + } + + public function setRgb($rgb) + { + if (strlen($rgb) < 6) { + for ($i = 0; $i < 7 - strlen($rgb); $i++) + $rgb = "0" . $rgb; + } + + if (strlen($rgb) > 6) { + $rgb = substr($rgb, strlen($rgb)-6, 6); + } + + $this->r = hexdec(substr($rgb, 0, 2)) / 255; + $this->g = hexdec(substr($rgb, 2, 2)) / 255; + $this->b = hexdec(substr($rgb, 4, 2)) / 255; + + $this->convertRgbToHsv(); + } + + public function getRgb() + { + return dechex($this->r*255) . dechex($this->g*255) . dechex($this->b*255); + } + + public function setHsv(array $hsv) + { + $this->h = $hsv['h']; + $this->s = $hsv['s']; + $this->v = $hsv['v']; + + $this->convertHsvToRgb(); + } + + public function getHsv() + { + return [ + 'h' => $this->h, + 's' => $this->s, + 'v' => $this->v, + ]; + } + + protected function convertHsvToRgb() + { + //1 + $h = $this->h * 6; + //2 + $i = floor($h); + $f = $h - $i; + //3 + $m = $this->v * (1 - $this->s); + $n = $this->v * (1 - $this->s * $f); + $k = $this->v * (1 - $this->s * (1 - $f)); + //4 + switch ($i) { + case 0: + list($this->r,$this->g,$this->b) = array($this->v,$k,$m); + break; + case 1: + list($this->r,$this->g,$this->b) = array($n,$this->v,$m); + break; + case 2: + list($this->r,$this->g,$this->b) = array($m,$this->v,$k); + break; + case 3: + list($this->r,$this->g,$this->b) = array($m,$n,$this->v); + break; + case 4: + list($this->r,$this->g,$this->b) = array($k,$m,$this->v); + break; + case 5: + case 6: //for when $H=1 is given + list($this->r,$this->g,$this->b) = array($this->v,$m,$n); + break; + } + + return $this; + } + + protected function convertRgbToHsv() + { + + $min = min($this->r, $this->g, $this->b); + $max = max($this->r, $this->g, $this->b); + $del = $max - $min; + + $v = $max; + + if ($del == 0) { + $h = 0; + $s = 0; + } else { + $s = $del / $max; + + $delR = ( ( ( $max - $this->r ) / 6 ) + ( $del / 2 ) ) / $del; + $delG = ( ( ( $max - $this->g ) / 6 ) + ( $del / 2 ) ) / $del; + $delB = ( ( ( $max - $this->b ) / 6 ) + ( $del / 2 ) ) / $del; + + if ($this->r == $max) $h = $delB - $delG; + else if ($this->g == $max) $h = ( 1 / 3 ) + $delR - $delB; + else if ($this->b == $max) $h = ( 2 / 3 ) + $delG - $delR; + + if ($h < 0) $h++; + if ($h > 1) $h--; + } + + $this->h = $h; + $this->s = $s; + $this->v = $v; + + return $this; + } +} diff --git a/ColorInterpolator/ColorInterpolator.php b/ColorInterpolator/ColorInterpolator.php new file mode 100644 index 0000000..1241e88 --- /dev/null +++ b/ColorInterpolator/ColorInterpolator.php @@ -0,0 +1,43 @@ + null, + 's' => null, + 'v' => null, + ]; + $color1Hsv = $color1->getHsv(); + $color2Hsv = $color2->getHsv(); + + foreach ($hsv as $key => $value) { + if ($color1Hsv[$key] < $color2Hsv[$key]) { + $outLength = 1 - $color2Hsv[$key] + $color1Hsv[$key]; + if ($key == 'h' && $outLength < 0.5) { + $hsv[$key] = $color2Hsv[$key] + $outLength * $part; + if ($hsv[$key] > 1) + $hsv[$key]--; + } else { + $hsv[$key] = ($color2Hsv[$key] - $color1Hsv[$key]) * $part + $color1Hsv[$key]; + } + } else { + $outLength = 1 - $color1Hsv[$key] + $color2Hsv[$key]; + if ($key == 'h' && $outLength < 0.5) { + $hsv[$key] = $color1Hsv[$key] + $outLength * $part; + if ($hsv[$key] > 1) + $hsv[$key]--; + } else { + $hsv[$key] = ($color1Hsv[$key] - $color2Hsv[$key]) * $part + $color2Hsv[$key]; + } + } + } + $color->setHsv($hsv); + + return $color; + } +} diff --git a/README.md b/README.md index e9f4433..78e7ddb 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,17 @@ -color-interpolator -================== +# Color Interpolator -Interpolate color in the spectral band +Interpolate color in the spectral band. + +## Usage + +```php +use Intaro\ColorInterpolator\Color; +use Intaro\ColorInterpolator\ColorInterpolator; + +// start rgb color +$startColor = new Color('5fb8df'); +// ending rgb color +$endingColor = new Color('3b9bcf'); + +$color = ColorInterpolator::interpolate($startColor, $endingColor); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f24eb9f --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "intaro/color-interpolator", + "description": "Interpolate color in the spectral band", + "license": "MIT", + "authors": [ + { + "name": "Intaro Soft", + "email": "mail@intaro.ru" + } + ], + "require": { + "php": ">=5.4.4" + }, + "autoload": { + "psr-0": { + "Intaro\\": "" + } + } +}