Phue\Helper\ColorConversion::convertRGBToXY PHP Method

convertRGBToXY() public static method

Converts RGB values to XY values Based on: http://stackoverflow.com/a/22649803
public static convertRGBToXY ( integer $red, integer $green, integer $blue ) : array
$red integer Red value
$green integer Green value
$blue integer Blue value
return array x, y, bri key/value
    public static function convertRGBToXY($red, $green, $blue)
    {
        // Normalize the values to 1
        $normalizedToOne['red'] = $red / 255;
        $normalizedToOne['green'] = $green / 255;
        $normalizedToOne['blue'] = $blue / 255;
        // Make colors more vivid
        foreach ($normalizedToOne as $key => $normalized) {
            if ($normalized > 0.04045) {
                $color[$key] = pow(($normalized + 0.055) / (1.0 + 0.055), 2.4);
            } else {
                $color[$key] = $normalized / 12.92;
            }
        }
        // Convert to XYZ using the Wide RGB D65 formula
        $xyz['x'] = $color['red'] * 0.664511 + $color['green'] * 0.154324 + $color['blue'] * 0.162028;
        $xyz['y'] = $color['red'] * 0.283881 + $color['green'] * 0.6684330000000001 + $color['blue'] * 0.047685;
        $xyz['z'] = $color['red'] * 0.0 + $color['green'] * 0.07231 + $color['blue'] * 0.986039;
        // Calculate the x/y values
        if (array_sum($xyz) == 0) {
            $x = 0;
            $y = 0;
        } else {
            $x = $xyz['x'] / array_sum($xyz);
            $y = $xyz['y'] / array_sum($xyz);
        }
        return array('x' => $x, 'y' => $y, 'bri' => round($xyz['y'] * 255));
    }

Usage Example

Example #1
0
 /**
  * Test: convert RGB to XY and brightness
  * 
  * @covers \Phue\Helper\ColorConversion::convertRGBToXY
  */
 public function testConvertRGBToXY()
 {
     // Values from: http://www.developers.meethue.com/documentation/hue-xy-values
     // Alice Blue
     $xy = ColorConversion::convertRGBToXY(239, 247, 255);
     $this->assertEquals(0.3088, $xy['x'], '', 0.0001);
     $this->assertEquals(0.3212, $xy['y'], '', 0.0001);
     $this->assertEquals(233, $xy['bri']);
     // Firebrick
     $xy = ColorConversion::convertRGBToXY(178, 33, 33);
     $this->assertEquals(0.6622, $xy['x'], '', 0.0001);
     $this->assertEquals(0.3024, $xy['y'], '', 0.0001);
     $this->assertEquals(35, $xy['bri']);
     // Medium Sea Green
     $xy = ColorConversion::convertRGBToXY(61, 178, 112);
     $this->assertEquals(0.1979, $xy['x'], '', 0.0001);
     $this->assertEquals(0.5004999999999999, $xy['y'], '', 0.0001);
     $this->assertEquals(81, $xy['bri']);
 }
All Usage Examples Of Phue\Helper\ColorConversion::convertRGBToXY