Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php function to convert RYB color to RGB color

Tags:

arrays

php

colors

I'm in php.

I've got a RYB color with that value :

$rybColor = array("r"=>0,"y"=255",b="255")

I would like to convert it in RGB in order to get

$rgbColor = array("r"=>0,"g"=>255,"b"=>0)

is that something possible ?

I found a script there in javascript link but it's a bit complicated for me. I stuck on the normalisation of the values..

like image 206
Odjone Avatar asked Oct 31 '25 02:10

Odjone


1 Answers

Absolutely.

Here's a quick PHP version of the JavaScript version of the Python version you linked:

// RYB color to RGB color
function RYB2RGB($iRed, $iYellow, $iBlue){

    // Remove the whiteness from the color.
    $iWhite = min($iRed, $iYellow, $iBlue);

    $iRed    -= $iWhite;
    $iYellow -= $iWhite;
    $iBlue   -= $iWhite;

    $iMaxYellow = max($iRed, $iYellow, $iBlue);

    // Get the green out of the yellow and blue
    $iGreen = min($iYellow, $iBlue);

    $iYellow -= $iGreen;
    $iBlue   -= $iGreen;

    if ($iBlue > 0 && $iGreen > 0)
    {
        $iBlue  *= 2.0;
        $iGreen *= 2.0;
    }

    // Redistribute the remaining yellow.
    $iRed   += $iYellow;
    $iGreen += $iYellow;

    // Normalize to values.
    $iMaxGreen = max($iRed, $iGreen, $iBlue);

    if ($iMaxGreen > 0)
    {
        $iN = $iMaxYellow / $iMaxGreen;

        $iRed   *= $iN;
        $iGreen *= $iN;
        $iBlue  *= $iN;
    }

    // Add the white back $in.
    $iRed   += $iWhite;
    $iGreen += $iWhite;
    $iBlue  += $iWhite;

    // Save the RGB
    $RGB = [floor($iRed), floor($iGreen), floor($iBlue)];

    return $RGB
}

$R = 98;
$y = 152;
$b = 223;

var_dump( RYB2RGB( $R,  $y, $b ) ); //

// array(3) {
//  [0]=>
//  float(98)
//  [1]=>
//  float(193)
//  [2]=>
//  float(223)
//   }
like image 74
Gavin Avatar answered Nov 01 '25 16:11

Gavin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!