0

I want to create a color pallet generator. At this point, I let the users enter the r, g and b values for a color. However, I have no idea how to use these values in a SASS variable. For example,

Let's say I am generating lighter version of colors like this : lighten($color, 10%);. Is there any way to set $color with JavaScript using these three variable I used in input above?

SanJeet Singh
  • 1,291
  • 2
  • 15
  • 28
  • Alternate duplicate: http://stackoverflow.com/questions/17787845/how-to-control-sass-variable-with-javascript – cimmanon Mar 05 '16 at 03:54

1 Answers1

0

It looks like this is impossible, since SASS information disappears once it is compiled to CSS.

Maybe lighten the color with Javascript instead? Then you can just set a JS variable. The following is taken from CSS Tricks:

function LightenDarkenColor(col, amt) {

    var usePound = false;

    if (col[0] == "#") {
        col = col.slice(1);
        usePound = true;
    }

    var num = parseInt(col,16);

    var r = (num >> 16) + amt;

    if (r > 255) r = 255;
    else if  (r < 0) r = 0;

    var b = ((num >> 8) & 0x00FF) + amt;

    if (b > 255) b = 255;
    else if  (b < 0) b = 0;

    var g = (num & 0x0000FF) + amt;

    if (g > 255) g = 255;
    else if (g < 0) g = 0;

    return (usePound?"#":"") + (g | (b << 8) | (r << 16)).toString(16);

}

// Lighten
var NewColor = LightenDarkenColor("#F06D06", 20); 

// Darken
var NewColor = LightenDarkenColor("#F06D06", -20); 
Community
  • 1
  • 1
steel
  • 11,883
  • 7
  • 72
  • 109