Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8343649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:07:50+00:00 2026-06-09T06:07:50+00:00

Okay, I have written a function that should convert HSL color value to RGB.

  • 0

Okay, I have written a function that should convert HSL color value to RGB. I have rewritten it in PHP according to this script: http://www.easyrgb.com/index.php?X=MATH&H=19#text19

This is what I have:

function HSL2RGB($h, $s, $l){
    function hue2rgb($v1, $v2, $vH){
        $sH = $vH;
        if($vH<0) $sH += 1;
        if($vH>1) $sH -= 1;
        if((6*$sH)<1) return $v1+($v2-$v1)*6*$sH;
        if((2*$sH)<1) return $v2;
        if((3*$sH)<2) return $v1+($v2-$v1)*((2/3)-$sH)*6;
        return $v1;
    }

    $h *= (5/18);
    $s /= 100;
    $l /= 100;
    $r=$g=$b=NULL;
    if($s==0){
        $r=$l*255;
        $g=$l*255;
        $b=$l*255;
    }else{
        if($l<0.5)
            $var_2 = $l*(1+$s);
        else
            $var_2 = ($l+$s)-($s*$l);

        $var_1 = 2*$l-$var_2;
        $r = 255*hue2rgb($var_1, $var_2, $h+(1/3));
        $g = 255*hue2rgb($var_1, $var_2, $h);
        $b = 255*hue2rgb($var_1, $var_2, $h-(1/3));
    }
    return array('r'=>$r,'g'=>$g,'b'=>$b);
}

var_dump(HSL2RGB(196.4, 100, 59.8));

The output of this script:

array(3) {
  ["r"]=>
  float(49.98)
  ["g"]=>
  float(49.98)
  ["b"]=>
  float(49.98)
}

The correct output is R: 50, G:199, B:255. I adapted it perfectly from the reference script from easyrgb.com. I just can’t figure out why it isn’t working. Any help would be awesome. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:07:51+00:00Added an answer on June 9, 2026 at 6:07 am

    According to the PHP manual, variable names are case-sensitive in PHP.

    Change $sh to $sH in your code.

    There may be an error in your PHP code.
    Add these lines at the beginning of your code:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    Why are you using $sH variable? The algorithm you point to is only using vH. Try replacing all $sH with $vH.

    In your original code, changing $h *= (5/18) to $h /= 360; will fix your function.

    There is one such fonction in the comments here: http://php.net/manual/en/function.imagecolorallocate.php

    function hslToRgb ($h, $s, $l) {
        if ($h>240 || $h<0) return array(0,0,0);
        if ($s>240 || $s<0) return array(0,0,0);
        if ($l>240 || $l<0) return array(0,0,0);    
        if ($h<=40) {
            $R=255;
            $G=(int)($h/40*256);
            $B=0;
        }
        elseif ($h>40 && $h<=80) {
            $R=(1-($h-40)/40)*256;
            $G=255;
            $B=0;
        }
        elseif ($h>80 && $h<=120) {
            $R=0;
            $G=255;
            $B=($h-80)/40*256;
        }
        elseif ($h>120 && $h<=160) {
            $R=0;
            $G=(1-($h-120)/40)*256;
            $B=255;
        }
        elseif ($h>160 && $h<=200) {
            $R=($h-160)/40*256;
            $G=0;
            $B=255;
        }
        elseif ($h>200) {
            $R=255;
            $G=0;
            $B=(1-($h-200)/40)*256;
        }
        $R=$R+(240-$s)/240*(128-$R);
        $G=$G+(240-$s)/240*(128-$G);
        $B=$B+(240-$s)/240*(128-$B);
        if ($l<120) {
            $R=($R/120)*$l;
            $G=($G/120)*$l;
            $B=($B/120)*$l;
        }
        else {
            $R=$l*((256-$R)/120)+2*$R-256;
            $G=$l*((256-$G)/120)+2*$G-256;
            $B=$l*((256-$B)/120)+2*$B-256;
        }
        if ($R<0) $R=0;
        if ($R>255) $R=255;
        if ($G<0) $G=0;
        if ($G>255) $G=255;
        if ($B<0) $B=0;
        if ($B>255) $B=255;
    
        return array((int)$R,(int)$G,(int)$B);
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this piece of code that splits a string and stores it
Okay - I'm beaten. I have a (PHP-CLI) server written using PHP's socket_* functions.
I have written a function match-rewriter that is essentially match-lambda except that it returns
Okay, so, I have a couple text files that are written to and read
Okay, so I have a register.php script written and I get an unexpected T
Okay this is one of those situations where I have concluded that I am
okay i have been trying to understand this for hours i am learning VB
Okay I have a large CRUD app that uses tabs with Forms embedded in
Okay so i have this code: if (isset($_GET['book'])) { $query= SELECT book_id, title, authors.`author`
Okay, so I've written a REST API implementation using mod_rewrite and PHP. I'm accepting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.