gheja

HSLa to RGBa js

Sep 1st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function hslaConvert(p, q, t)
  2. {
  3.     if (t < 0) t += 1;
  4.     if (t > 1) t -= 1;
  5.     if (t < 1/6) return p + (q - p) * 6 * t;
  6.     if (t < 3/6) return q;
  7.     if (t < 4/6) return p + (q - p) * 6 * (4/6 - t);
  8.     return p
  9. }
  10.  
  11. function hsla2rgba(h, s, l, a)
  12. {
  13.     // thanks Mohsen! https://stackoverflow.com/a/9493060/460571
  14.     let p, q, r, g, b;
  15.    
  16.     if (l < 0.5)
  17.     {
  18.         q = l * (1 + s);
  19.     }
  20.     else
  21.     {
  22.         q = l + s - l * s;
  23.     }
  24.    
  25.     p = 2 * l - q;
  26.    
  27.     r = Math.floor(hslaConvert(p, q, h + 1/3) * 256);
  28.     g = Math.floor(hslaConvert(p, q, h) * 256);
  29.     b = Math.floor(hslaConvert(p, q, h - 1/3) * 256);
  30.    
  31.     return [ r, g, b, a ];
  32. }
  33.  
  34. function hsla2rgba_(h, s, l, a)
  35. {
  36.     let c;
  37.    
  38.     c = hsla2rgba(h, s, l, a);
  39.    
  40.     return "rgba(" + c[0] + "," + c[1] + "," + c[2] + "," + c[3] + ")";
  41. }
Add Comment
Please, Sign In to add comment