1

Good morning. I have found the following script that converts colors in HSL (Hue, Saturation, Level) format to RGB (Red,Green,Blue) format in this thread: HSL to RGB color conversion

/**
 * Converts an HSL color value to RGB. Conversion formula
 * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
 * Assumes h, s, and l are contained in the set [0, 1] and
 * returns r, g, and b in the set [0, 255].
 *
 * @param   {number}  h       The hue
 * @param   {number}  s       The saturation
 * @param   {number}  l       The lightness
 * @return  {Array}           The RGB representation
 */
function hslToRgb(h, s, l){
    var r, g, b;

    if(s == 0){
        r = g = b = l; // achromatic
    }else{
        var hue2rgb = function hue2rgb(p, q, t){
            if(t < 0) t += 1;
            if(t > 1) t -= 1;
            if(t < 1/6) return p + (q - p) * 6 * t;
            if(t < 1/2) return q;
            if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
            return p;
        }

        var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
        var p = 2 * l - q;
        r = hue2rgb(p, q, h + 1/3);
        g = hue2rgb(p, q, h);
        b = hue2rgb(p, q, h - 1/3);
    }

    return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}

I would like to know whether it is possible to use it, given the necessary adaptations, in the Google Sheets environment as an Apps Script. The idea would be to paste HSL values in comma separated format (H,S,L) in column A:A, run the script and get the RGB values also in comma separated format (R,G,B) in column B:B. Thank you so much.

  • 2
    Yes, you should be able to. Assuming the logic is correct (I'd assume it is), then I don't see a problem. I'm not very familiar with GAP but I'd expect it to support everything here. – VLAZ May 05 '20 at 06:44

1 Answers1

2

I believe your goal as follows.

  • There are the values of HSL to the column "A". Each cell has the comma separated value like 0.5,0.5,0.5.
  • You want to use your custom function like =hslToRgb(A:A) put in the cell "B1", and you want to put the result values to the column "B". Each cell has the comma separated value like 64,191,191.

For this, how about this answer?

Modification points:

  • I think that the logic of your script is correct.
  • When =hslToRgb(A:A) is used, the values from A:A is the 2 dimensional array. In your current script, each value is used as each argument.

Using this, I prepared a wrapper function for your script as follows.

Modified script:

Please copy and paste the following script. In your script, only the function name is changed for using =hslToRgb(A:A). Please be careful this.

// I prepared this function.
function hslToRgb(values) {
  return values.map(([a]) => (
    [a != "" ? hslToRgb_calc_(...a.split(",").map(e => Number(e.trim()))).join(",") : null]
  ));
}

function hslToRgb_calc_(h, s, l){  // Modified
    var r, g, b;

    if(s == 0){
        r = g = b = l; // achromatic
    }else{
        var hue2rgb = function hue2rgb(p, q, t){
            if(t < 0) t += 1;
            if(t > 1) t -= 1;
            if(t < 1/6) return p + (q - p) * 6 * t;
            if(t < 1/2) return q;
            if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
            return p;
        }

        var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
        var p = 2 * l - q;
        r = hue2rgb(p, q, h + 1/3);
        g = hue2rgb(p, q, h);
        b = hue2rgb(p, q, h - 1/3);
    }

    return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
  • In order to use this script, please put =hslToRgb(A:A) to the cell "B1". In this case, it supposes that the cells "A:A" have the comma separated HSL values.
    • When the value is "0.5,0.5,0.5" of HSL value as the string, "64,191,191" of RGB value is returned as the string.

Note:

  • Please use this script with V8.

References:

Tanaike
  • 105,090
  • 8
  • 51
  • 83