From f7f5c353b9eff152ab42201ec4ef8438f616e41e Mon Sep 17 00:00:00 2001 From: Takashi Irie Date: Fri, 29 Mar 2013 20:16:59 +0000 Subject: [PATCH] Make sure Tonesque works with PHP 5.2 also --- class.color.php | 51 ++++++++++++++++++++++++++----------------------- tonesque.php | 3 ++- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/class.color.php b/class.color.php index 0832ffc..9989ff6 100644 --- a/class.color.php +++ b/class.color.php @@ -206,9 +206,7 @@ public function toRgbInt() */ public function toRgbHex() { - return array_map(function($item){ - return dechex($item); - }, $this->toRgbInt()); + return array_map( 'dechex', $this->toRgbInt() ); } /** @@ -398,18 +396,9 @@ public function toXyz() $rgb = $this->toRgbInt(); // Normalize RGB values to 1 - $rgb = array_map(function($item){ - return $item / 255; - }, $rgb); - - $rgb = array_map(function($item){ - if ($item > 0.04045) { - $item = pow((($item + 0.055) / 1.055), 2.4); - } else { - $item = $item / 12.92; - } - return ($item * 100); - }, $rgb); + $rgb = array_map( array( &$this, 'normalizeRgb' ) , $rgb ); + + $rgb = array_map( array( &$this, 'toXyz_calc' ) , $rgb ); //Observer. = 2°, Illuminant = D65 $xyz = array( @@ -435,14 +424,7 @@ public function toLabCie() $xyz['y'] /= 100; $xyz['z'] /= 108.883; - $xyz = array_map(function($item){ - if ($item > 0.008856) { - //return $item ^ (1/3); - return pow($item, 1/3); - } else { - return (7.787 * $item) + (16 / 116); - } - }, $xyz); + $xyz = array_map( array( &$this, 'toLabCie_calc' ) , $xyz ); $lab = array( 'l' => (116 * $xyz['y']) - 16, @@ -741,4 +723,25 @@ public function incrementHue( $amount ) { return $this->fromHsl( $h, $s, $l ); } -} // class Color + public function normalizeRgb( $item ) { + return $item / 255; + } + + public function toXyz_calc( $item ) { + if ( $item > 0.04045 ) { + $item = pow( ( ( $item + 0.055 ) / 1.055 ), 2.4 ); + } else { + $item = $item / 12.92; + } + return ( $item * 100 ); + } + + public function toLabCie_calc( $item ) { + if ( $item > 0.008856 ) { + //return $item ^ (1/3); + return pow( $item, 1/3 ); + } else { + return ( 7.787 * $item ) + ( 16 / 116 ); + } + } +} // class Color \ No newline at end of file diff --git a/tonesque.php b/tonesque.php index 1fa831f..4121ef0 100644 --- a/tonesque.php +++ b/tonesque.php @@ -16,7 +16,8 @@ class Tonesque { private $color = ''; function __construct( $image ) { - require_once __DIR__ . '/class.color.php'; + if ( ! class_exists( 'Color' ) ) + require_once dirname( __FILE__ ) . '/class.color.php'; $this->image = esc_url_raw( $image ); }