spatie / color

A little library to deal with color conversions
https://spatie.be/opensource/php
MIT License
350 stars 36 forks source link

support convert hsl hsv cmyk? #32

Closed StringKe closed 2 years ago

sebastiandedeyne commented 4 years ago

HSL support has been added in #33 :)

sebastiandedeyne commented 4 years ago

For other color systems: PR's welcome!

StringKe commented 4 years ago

Hex: #530005ff not support ff = round(hexdec(substr($hex, 6, 2)) / 255, 2)

my convert alpha hex code:

$color = "#530005ff";
if (strpos($color, '#') !== false) {
            $hex = str_replace('#', '', $color);
            $length = strlen($hex);
            $r = 255;
            $g = 255;
            $b = 255;
            $a = 1;
            if ($length === 3) {
                $r = hexdec(substr($color, 0, 2));
                $g = hexdec(substr($color, 2, 2));
                $b = hexdec(substr($color, 4, 2));
            } else if ($length >= 6) {
                $r = hexdec($length >= 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0));
                $g = hexdec($length >= 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0));
                $b = hexdec($length >= 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0));
            }
            if ($length == 8) {
                $a = round(hexdec(substr($hex, 6, 2)) / 255, 2);
            }
            return 'rgba(' . $r . ',' . $g . ',' . $b . ',' . $a . ')';
        }

Hope to help your