calculates the YIQ value for a given color.

@param RGB $color

@return float 0-255
190956
private function calculateYIQValue(RGB $color)
    {
        $yiq = (($color->getRed() * 299) + ($color->getGreen() * 587) + ($color->getBlue() * 114)) / 1000;
        return $yiq;
    }
238237
public function getColorIndex($color)
    {
        $color = self::getColorAsFullHexCode($color);
        $index = array_search($color, $this->_colors);
        return $index !== false ? $index + 1 : false;
    }
1301197
public function rgbXY($x, $y) {
        $color = $this->colorXY($x, $y);
        return array(($color >> 16) & 0xFF, ($color >> 8) & 0xFF, $color & 0xFF);
    }
331583
private static function _get_hue_based_max_rgb( $red, $green, $blue ) {
		$max_rgb = max( $red, $green, $blue );
		$min_rgb = min( $red, $green, $blue );

		$diff_max_min_rgb = $max_rgb - $min_rgb;

		if ( $red === $max_rgb ) {
			$hue = 60 * ( $diff_max_min_rgb ? ( $green - $blue ) / $diff_max_min_rgb : 0 );
		} elseif ( $green === $max_rgb ) {
			$hue = 60 * ( $diff_max_min_rgb ? ( $blue - $red ) / $diff_max_min_rgb : 0 ) + 120;
		} elseif ( $blue === $max_rgb ) {
			$hue = 60 * ( $diff_max_min_rgb ? ( $red - $green ) / $diff_max_min_rgb : 0 ) + 240;
		}

		return $hue;
	}
890092
public function getDiff($color)
    {
        if (!$color instanceof Color) {
            $color = new Color($color);
        }
        return pow($this->r - $color->r, 2)
            + pow($this->g - $color->g, 2)
            + pow($this->b - $color->b, 2);
    }
137074
public function getRgbaString(Color $color)
    {
        return 'rgba('. $color->red . ', ' . $color->green . ', ' . $color->blue . ', ' . $color->alpha . ')';
    }
1045444
public function validateColor($color)
    {
        $request = new Request('GET', 'color');
        $response = $this->sendRequest($request, [
            'query' => [
                'string' => $color,
            ]
        ]);

        return $response->getBody();
    }
770370
public static float[] cmykFromRgb(int rgbColor) {
        int red = (0xff0000 & rgbColor) >> 16;
        int green = (0xff00 & rgbColor) >> 8;
        int blue = (0xff & rgbColor);
        float black = Math.min(1.0f - red / 255.0f, Math.min(1.0f - green / 255.0f, 1.0f - blue / 255.0f));
        float cyan = 1.0f;
        float magenta = 1.0f;
        float yellow = 1.0f;
        if (black != 1.0f) {
            // black 1.0 causes zero divide
            cyan = (1.0f - (red / 255.0f) - black) / (1.0f - black);
            magenta = (1.0f - (green / 255.0f) - black) / (1.0f - black);
            yellow = (1.0f - (blue / 255.0f) - black) / (1.0f - black);
        }
        return new float[]{cyan, magenta, yellow, black};
    }
273403
public static function calculateLuminance($color)
	{
		$rgb = static::getMixedColorAsRgb($color);
		if ($rgb === false) return false;

		return 0.2126 * $rgb[0] + 0.7152 * $rgb[1] + 0.0722 * $rgb[2];
	}
596518
public function image_colorize($rgb)
	{
		imageTrueColorToPalette($this->imageResized, true, 256);
		$numColors = imageColorsTotal($this->imageResized);

		for ($x = 0; $x < $numColors; $x++)
		{
			list($r, $g, $b) = array_values(imageColorsForIndex($this->imageResized, $x));

			// calculate grayscale in percent
			$grayscale = ($r + $g + $b) / 3 / 0xff;

			imageColorSet($this->imageResized, $x,
				$grayscale * $rgb[0],
				$grayscale * $rgb[1],
				$grayscale * $rgb[2]
			);

		}

		return true;
	}
1074940
public static function foreground($color)
    {
        list($red, $green, $blue) = str_split(substr($color, 1), 2);

        $red   = hexdec($red);
        $green = hexdec($green);
        $blue  = hexdec($blue);

        return ($red < 200 && $green < 100 && $blue < 200) ? self::WHITE : self::BLACK;
    }
310941
private function _addColor($rgb) {
		if (!isset($this->_colors[$rgb])) {
			if (count($this->_colors) < 57) {
				// then we add a custom color altering the palette
				$colorIndex = 8 + count($this->_colors);
				$this->_palette[$colorIndex] =
					array(
						hexdec(substr($rgb, 0, 2)),
						hexdec(substr($rgb, 2, 2)),
						hexdec(substr($rgb, 4)),
						0
					);
				$this->_colors[$rgb] = $colorIndex;
			} else {
				// no room for more custom colors, just map to black
				$colorIndex = 0;
			}
		} else {
			// fetch already added custom color
			$colorIndex = $this->_colors[$rgb];
		}

		return $colorIndex;
	}
771133
public static function load($color, $resource)
    {
        if ($color === 'transparent') {

            return imagecolorallocatealpha($resource, 0, 0, 0, 127);
        }

        $gdpixel = new GDPixel($color);

        return imagecolorallocate($resource, $gdpixel->channelR, $gdpixel->channelG, $gdpixel->channelB);
    }
1216342
public function setFromValue($rgb, $hasalpha = true)
    {
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = ($rgb >> 0) & 0xFF;
        if ($hasalpha) {
            $a = ($rgb >> 24) & 0xff;

            return $this->setRed($r)
                            ->setGreen($g)
                            ->setBlue($b)
                            ->setAlpha($a);
        }

        return $this->setRed($r)
                        ->setGreen($g)
                        ->setBlue($b);
    }
1004748
function _calculateBlendvalue($mode, $colorrgb1, $colorrgb2)
    {
        switch ($mode)
        {
            case IMAGE_TOOLBOX_BLEND_MULTIPLY:
                $c['red'] = ($colorrgb1['red'] * $colorrgb2['red']) >> 8;
                $c['green'] = ($colorrgb1['green'] * $colorrgb2['green']) >> 8;
                $c['blue'] = ($colorrgb1['blue'] * $colorrgb2['blue']) >> 8;
                break;

            case IMAGE_TOOLBOX_BLEND_SCREEN:
                $c['red'] = 255 - ((255 - $colorrgb1['red']) * (255 - $colorrgb2['red']) >> 8);
                $c['green'] = 255 - ((255 - $colorrgb1['green']) * (255 - $colorrgb2['green']) >> 8);
                $c['blue'] = 255 - ((255 - $colorrgb1['blue']) * (255 - $colorrgb2['blue']) >> 8);
                break;

            case IMAGE_TOOLBOX_BLEND_DIFFERENCE:
                $c['red'] = abs($colorrgb1['red'] - $colorrgb2['red']);
                $c['green'] = abs($colorrgb1['green'] - $colorrgb2['green']);
                $c['blue'] = abs($colorrgb1['blue'] - $colorrgb2['blue']);
                break;

            case IMAGE_TOOLBOX_BLEND_NEGATION:
                $c['red'] = 255 - abs(255 - $colorrgb1['red'] - $colorrgb2['red']);
                $c['green'] = 255 - abs(255 - $colorrgb1['green'] - $colorrgb2['green']);
                $c['blue'] = 255 - abs(255 - $colorrgb1['blue'] - $colorrgb2['blue']);
                break;

            case IMAGE_TOOLBOX_BLEND_EXCLUSION:
                $c['red'] = $colorrgb1['red'] + $colorrgb2['red'] - (($colorrgb1['red'] * $colorrgb2['red']) >> 7);
                $c['green'] = $colorrgb1['green'] + $colorrgb2['green'] - (($colorrgb1['green'] * $colorrgb2['green']) >> 7);
                $c['blue'] = $colorrgb1['blue'] + $colorrgb2['blue'] - (($colorrgb1['blue'] * $colorrgb2['blue']) >> 7);
                break;

            case IMAGE_TOOLBOX_BLEND_OVERLAY:
                if ($colorrgb1['red'] < 128)
                {
                    $c['red'] = ($colorrgb1['red'] * $colorrgb2['red']) >> 7;
                }
                else
                {
                    $c['red'] = 255 - ((255 - $colorrgb1['red']) * (255 - $colorrgb2['red']) >> 7);
                }
                if ($colorrgb1['green'] < 128)
                {
                    $c['green'] = ($colorrgb1['green'] * $colorrgb2['green']) >> 7;
                }
                else
                {
                    $c['green'] = 255 - ((255 - $colorrgb1['green']) * (255 - $colorrgb2['green']) >> 7);
                }
                if ($colorrgb1['blue'] < 128)
                {
                    $c['blue'] = ($colorrgb1['blue'] * $colorrgb2['blue']) >> 7;
                }
                else
                {
                    $c['blue'] = 255 - ((255 - $colorrgb1['blue']) * (255 - $colorrgb2['blue']) >> 7);
                }
                break;

            default:
                break;
        }
        return $c;
    }
20874
public function setFromRGBColor(RGBColor $color)
    {
        return $this->setRed($color->getRed())
                        ->setGreen($color->getGreen())
                        ->setBlue($color->getBlue())
                        ->setAlpha($color->getAlpha());
    }
12521
public function setColor($color)
    {
        $allowed_values = array('#ffffff', '#ffff01', '#ff6600', '#fe0000', '#9a0000', '#660066', '#0000fe', '#0099ff', '#01ffff', '#00ff33', '#349800', '#006634', '#666666', '#999999', '#e6e6e6', '#ffffcd', '#99ffff', '#ccff9a');
        if (!is_null($color) && (!in_array($color, $allowed_values))) {
            throw new \InvalidArgumentException("Invalid value for 'color', must be one of '#ffffff', '#ffff01', '#ff6600', '#fe0000', '#9a0000', '#660066', '#0000fe', '#0099ff', '#01ffff', '#00ff33', '#349800', '#006634', '#666666', '#999999', '#e6e6e6', '#ffffcd', '#99ffff', '#ccff9a'");
        }
        $this->container['color'] = $color;

        return $this;
    }