Example #1
0
function hsl2rgb($h = 60, $s = 75, $l = 50)
{
    // input: HSL color; output: RGB color (array)
    $h = fmod(fmod($h, 360) + 360, 360);
    // normalizing $hue to degree (0-359)
    //$h = $h/360;
    $h = $h / 359;
    if ($s < 0) {
        $s = 0;
    }
    if ($s > 100) {
        $s = 100;
    }
    $s = $s / 100;
    if ($l < 0) {
        $l = 0;
    }
    if ($l > 100) {
        $l = 100;
    }
    $l = $l / 100;
    //printf('%5.45F',$s); echo '<br>';
    if ($s == 0.0) {
        $r = $g = $b = $l;
    } else {
        if ($l <= 0.5) {
            $m2 = $l * ($s + 1);
        } else {
            $m2 = $l + $s - $l * $s;
        }
        $m1 = $l * 2 - $m2;
        $r = hue($m1, $m2, $h + 1 / 3);
        $g = hue($m1, $m2, $h);
        $b = hue($m1, $m2, $h - 1 / 3);
    }
    $rgb = array(round($r * 255), round($g * 255), round($b * 255));
    return $rgb;
}
function shifthue($image, $mode, $R, $G, $B)
{
    //hue() function over every pixel of the image; goes for every pixel;
    $img = imagecreatefrompng($image);
    if ($R != NULL && $G != NULL && $B != NULL) {
        $msk = file_get_contents($image . ".txt");
        //$msk is a textfile that say which pixel should stay untouched
        $t = 0;
        //index for the mask
        $size = getimagesize($image);
        for ($i = 0; $i < $size[0]; $i++) {
            for ($j = 0; $j < $size[1]; $j++) {
                if ($msk[$t] == "1" || $msk == NULL) {
                    //draw a pixel if there is no mask file OR if the char in the mask says "1"
                    $rgb = imagecolorsforindex($img, imagecolorat($img, $i, $j));
                    //colors from the current pixel are red
                    $R1 = $rgb['red'];
                    $G1 = $rgb['green'];
                    $B1 = $rgb['blue'];
                    $hue = hue($R1, $G1, $B1, $R, $G, $B, $mode);
                    //new color is calculated
                    $colorInt = round($hue[2]);
                    $colorInt += round($hue[1]) << 8;
                    $colorInt += round($hue[0]) << 16;
                    //RGB is saved in a int so we can give it to "imagesetpixel()"
                    imagesetpixel($img, $i, $j, $colorInt);
                }
                $t++;
            }
        }
    }
    return $img;
}
Example #3
0
 */
$file = __DIR__ . '/../colors.json';
$color_json = file_get_contents($file);
$colors = json_decode($color_json, true);
rename($file, $file . '.old');
foreach ($colors as &$color) {
    extract($color);
    list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
    // Normalize rgb
    $r /= 255;
    $g /= 255;
    $b /= 255;
    // Calculate min and max values
    $min = min($r, $g, $b);
    $max = max($r, $g, $b);
    $color['hue'] = (360 + hue($r, $g, $b, $min, $max)) % 360;
    $color['saturation'] = saturation($min, $max);
    $color['value'] = $max;
    $color['lightness'] = ($max + $min) / 2;
}
file_put_contents(__DIR__ . '/../colors.json', json_encode($colors));
function hue($r, $g, $b, $min, $max)
{
    if ($max == $min) {
        return 0;
    }
    $diff = $max - $min;
    if ($max == $r) {
        return 60 * (0 + ($g - $b) / $diff);
    }
    if ($max == $g) {