hsl转rgb java_HSL到RGB颜色转换

Garry Tan发布了一个Javascript解决方案他的博客(他把这归功于一家现已停业的mjiJackson.com,但在这里存档和原作者有一个要点-感谢用户2441511)。

代码重新发布如下:

HSL到RGB:/**

* 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 

if(t > 1) t -= 1;

if(t 

if(t 

if(t 

return p;

}

var q = l 

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)];}

RGB到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 

case g: h = (b - r) / d + 2; break;

case b: h = (r - g) / d + 4; break;

}

h /= 6;

}

return [h, s, l];}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值