Phue\Helper\ColorConversion::convertXYToRGB PHP Method

convertXYToRGB() public static method

Converts XY (and brightness) values to RGB
public static convertXYToRGB ( float $x, float $y, integer $bri = 255 ) : array
$x float X value
$y float Y value
$bri integer Brightness value
return array red, green, blue key/value
    public static function convertXYToRGB($x, $y, $bri = 255)
    {
        // Calculate XYZ
        $z = 1.0 - $x - $y;
        $xyz['y'] = $bri / 255;
        $xyz['x'] = $xyz['y'] / $y * $x;
        $xyz['z'] = $xyz['y'] / $y * $z;
        // Convert to RGB using Wide RGB D65 conversion
        $color['red'] = $xyz['x'] * 1.656492 - $xyz['y'] * 0.354851 - $xyz['z'] * 0.255038;
        $color['green'] = -$xyz['x'] * 0.707196 + $xyz['y'] * 1.655397 + $xyz['z'] * 0.036152;
        $color['blue'] = $xyz['x'] * 0.051713 - $xyz['y'] * 0.121364 + $xyz['z'] * 1.01153;
        foreach ($color as $key => $normalized) {
            // Apply reverse gamma correction
            if ($normalized <= 0.0031308) {
                $color[$key] = 12.92 * $normalized;
            } else {
                $color[$key] = (1.0 + 0.055) * pow($normalized, 1.0 / 2.4) - 0.055;
            }
            // Scale back from a maximum of 1 to a maximum of 255
            $color[$key] = round($color[$key] * 255);
        }
        return $color;
    }

Usage Example

Example #1
0
 /**
  * Test: convert XY and brightness to RGB
  *
  * @covers \Phue\Helper\ColorConversion::convertXYToRGB
  */
 public function testConvertXYToRGB()
 {
     // Conversion back from the test above
     // Alice Blue
     $rgb = ColorConversion::convertXYToRGB(0.3088, 0.3212, 233);
     $this->assertEquals($rgb['red'], 239);
     $this->assertEquals($rgb['green'], 247);
     $this->assertEquals($rgb['blue'], 255);
     // Firebrick
     $rgb = ColorConversion::convertXYToRGB(0.6622, 0.3024, 35);
     $this->assertEquals($rgb['red'], 178);
     $this->assertEquals($rgb['green'], 33);
     $this->assertEquals($rgb['blue'], 33);
     // Medium Sea Green
     $rgb = ColorConversion::convertXYToRGB(0.1979, 0.5004999999999999, 81);
     $this->assertEquals($rgb['red'], 61);
     $this->assertEquals($rgb['green'], 178);
     $this->assertEquals($rgb['blue'], 112);
 }
All Usage Examples Of Phue\Helper\ColorConversion::convertXYToRGB