Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript to convert rgb to hsl

/**
 * Converts an RGB color value to HSL. Conversion formula
 * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
 * Assumes r, g, and b are contained in the set [0, 255] and
 * returns h, s, and l in the set [0, 1].
 *
 * @param   {number}  r       The red color value
 * @param   {number}  g       The green color value
 * @param   {number}  b       The blue color value
 * @return  {Array}           The HSL representation
 */
function rgbToHsl(r, g, b){
    r /= 255, g /= 255, b /= 255;
    var max = Math.max(r, g, b), min = Math.min(r, g, b);
    var h, s, l = (max + min) / 2;

    if(max == min){
        h = s = 0; // achromatic
    }else{
        var d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        switch(max){
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
    }

    return [h, s, l];
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: get window resolution javascript 
Javascript :: setlocalstorage 
Javascript :: node js sublime text 
Javascript :: jquery add td to tr dynamically 
Javascript :: check if js string begin with word 
Javascript :: round a number to fixed decimals 
Javascript :: change background color input jquery 
Javascript :: js arrotondare numeri 
Javascript :: email validation regex 
Javascript :: javascript length of number 
Javascript :: how do i get month and date of javascript in 2 digit format 
Javascript :: get screen width javascript 
Javascript :: Count Backwards With a For Loop 
Javascript :: retrieve object array value based on key 
Javascript :: corresponding text to key code js 
Javascript :: orthographic camera three js 
Javascript :: js string find regex 
Javascript :: jquery disable form element 
Javascript :: javascript remove all child elements 
Javascript :: how to encode a string in javascript 
Javascript :: js sort by name 
Javascript :: capital first letter react 
Javascript :: regex min length max length 
Javascript :: location.reload sweetalert 
Javascript :: regex to check the phone number javascript 
Javascript :: neo4j delete relationship nodes 
Javascript :: javascript add required to input 
Javascript :: get attribute href 
Javascript :: array sort by alphabetical javascript 
Javascript :: go to new page javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =