Get a light or dark color based on the contrast required for a good readability

@param string $color
@param string $dark
@param string $light
@return string
179722
public function getLightOrDark($color, $dark = '#000000', $light = '#FFFFFF')
    {
        $color = str_replace('#', '', $this->hex6($color));

        $r = hexdec(substr($color, 0, 2));
        $g = hexdec(substr($color, 2, 2));
        $b = hexdec(substr($color, 4, 2));

        $brightness = (($r * 299) + ($g * 587) + ($b * 114)) / 1000;

        return ($brightness >= 155) ? $dark : $light;
    }
1108132
public static function blue($color, $set = null)
    {
        $mode = '';
        $color = self::normalizeColor($color, $mode);
        if (null !== $set) {
            $newColor = [
                $color[0],
                $color[1],
                $set,
            ];
            if (self::hasAlpha($color)) {
                $newColor[3] = $color[3];
            }

            return self::resolveColor($newColor, $mode);
        }

        return $color[2];
    }
534601
public function luminosityContrast(Color $color): float
    {
        $colorLuminosity1 = $this->getLuminosity() + 0.05;
        $colorLuminosity2 = $color->getLuminosity() + 0.05;

        return max($colorLuminosity1, $colorLuminosity2) / min($colorLuminosity1, $colorLuminosity2);
    }
763463
protected function background($color = null)
    {
        if (!is_null($color)) {
            $this->resource->setImageBackgroundColor(sprintf('#%s', $color));
        }
        return $this;
    }
605306
protected function parse_color($color) {
        if (strpos($color, '#') === 0) {
            // No conversion should be needed.
            return $color;
        }

        if ($color > 7 and $color < 53) {
            $numbers = array(
                8  => 'black',
                12 => 'blue',
                16 => 'brown',
                15 => 'cyan',
                23 => 'gray',
                17 => 'green',
                11 => 'lime',
                14 => 'magenta',
                18 => 'navy',
                53 => 'orange',
                33 => 'pink',
                20 => 'purple',
                10 => 'red',
                22 => 'silver',
                9  => 'white',
                13 => 'yellow',
            );
            if (isset($numbers[$color])) {
                $color = $numbers[$color];
            } else {
                $color = 'black';
            }
        }

        $colors = array(
            'aqua'    => '00FFFF',
            'black'   => '000000',
            'blue'    => '0000FF',
            'brown'   => 'A52A2A',
            'cyan'    => '00FFFF',
            'fuchsia' => 'FF00FF',
            'gray'    => '808080',
            'grey'    => '808080',
            'green'   => '00FF00',
            'lime'    => '00FF00',
            'magenta' => 'FF00FF',
            'maroon'  => '800000',
            'navy'    => '000080',
            'orange'  => 'FFA500',
            'olive'   => '808000',
            'pink'    => 'FAAFBE',
            'purple'  => '800080',
            'red'     => 'FF0000',
            'silver'  => 'C0C0C0',
            'teal'    => '008080',
            'white'   => 'FFFFFF',
            'yellow'  => 'FFFF00',
        );

        if (isset($colors[$color])) {
            return('#'.$colors[$color]);
        }

        return('#'.$colors['black']);
    }
222864
public static function lighten($color, $percent)
    {
        $mode = '';
        $color = self::normalizeColor($color, $mode);
        for ($i = 0; $i <= 2; ++$i) {
            $color[$i] = round($color[$i] + (self::COLOR_PERCENT * $percent));
            if ($color[$i] > 255) {
                $color[$i] = 255;
            }
        }

        return self::resolveColor($color, $mode);
    }
275697
function(fillColor) {
            var fillColorHex = this.colorUtils.hexFromColor(fillColor),
                luminanceThreshold = 128,
                darkColor = 'black',
                lightColor = 'white',
                fillLuminance = this.colorUtils.getLuminance(fillColorHex);

            return (fillLuminance < luminanceThreshold) ? lightColor : darkColor;
        }
1258394
protected function normalize_color($color) {
        if (is_string($color)) {
            $color = trim($color, '#');
            if (strlen($color) == 6) {
                list($r, $g, $b) = array(
                    $color[0] . $color[1],
                    $color[2] . $color[3],
                    $color[4] . $color[5]
                );
            } elseif (strlen($color) == 3) {
                list($r, $g, $b) = array(
                    $color[0] . $color[0],
                    $color[1] . $color[1],
                    $color[2] . $color[2]
                );
            } else {
                return false;
            }
            return array(
                'r' => hexdec($r),
                'g' => hexdec($g),
                'b' => hexdec($b),
                'a' => 0
            );
        } elseif (is_array($color) && (count($color) == 3 || count($color) == 4)) {
            if (isset($color['r'], $color['g'], $color['b'])) {
                return array(
                    'r' => $this->keep_within($color['r'], 0, 255),
                    'g' => $this->keep_within($color['g'], 0, 255),
                    'b' => $this->keep_within($color['b'], 0, 255),
                    'a' => $this->keep_within(isset($color['a']) ? $color['a'] : 0, 0, 127)
                );
            } elseif (isset($color[0], $color[1], $color[2])) {
                return array(
                    'r' => $this->keep_within($color[0], 0, 255),
                    'g' => $this->keep_within($color[1], 0, 255),
                    'b' => $this->keep_within($color[2], 0, 255),
                    'a' => $this->keep_within(isset($color[3]) ? $color[3] : 0, 0, 127)
                );
            }
        }
        return false;
    }
998638
def get_darker_color(self):
        """"""
        # we change a bit the color in one direction
        if bw_contrasted(self._true_color, 30) == WHITE:
            color = mix(self._true_color, WHITE, 0.9)
        else:
            color = mix(self._true_color, BLACK, 0.9)

        return color
108161
public static function name(Color $color): string
    {
        if (!self::hasName($color)) {
            throw new ColorException('No matching CSS color name found for ' . self::hex($color));
        }

        return X11Colors::$colors[$color->getHex()][0];
    }
736236
static function resolveColor($color) {
        if(\is_int($color)) {
            return $color;
        }
        
        if(!\is_array($color)) {
            return \hexdec(((string) $color));
        }
        
        if(\count($color) < 1) {
            throw new \InvalidArgumentException('Color "'.\var_export($color, true).'" is not resolvable');
        }
        
        return (($color[0] << 16) + (($color[1] ?? 0) << 8) + ($color[2] ?? 0));
    }
1118462
public static function fromHex(string $color): Color
    {
        if (!Validator::isValidHex($color)) {
            throw new InvalidColorException('Invalid hex value');
        }

        return new self(...array_values(self::hexToRgb($color)));
    }
830800
protected function background($color = null)
    {
        if (is_string($color)) {
            $this->commands['-background "#%s"'] = [trim($color, '#')];
        }
        return $this;
    }
644569
function getcolors($color)
    {
        $color = str_replace('#', '', $color);
        if (strlen($color) == 3) {
            $color = str_repeat(substr($color, 0, 1), 2) . str_repeat(substr($color, 1, 1), 2) . str_repeat(substr($color, 2, 1), 2);
        }
        $r     = sscanf($color, "%2x%2x%2x");
        $red   = (is_array($r) && array_key_exists(0, $r) && is_numeric($r[0]) ? $r[0] : 0);
        $green = (is_array($r) && array_key_exists(1, $r) && is_numeric($r[1]) ? $r[1] : 0);
        $blue  = (is_array($r) && array_key_exists(2, $r) && is_numeric($r[2]) ? $r[2] : 0);

        return [$red, $green, $blue];
    }
1038406
public function getBrightnessDifference($color1, $color2): float
    {
        $c1 = color($color1);
        $c2 = color($color2);
        return brightness_difference($c1, $c2);
    }
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;
    }
1111751
public static ColorUIResource darken(ColorUIResource color)
    {
        return new ColorUIResource(darkenRGB(color.getRed()), darkenRGB(color.getGreen()), darkenRGB(color.getBlue()));
    }