[英]how to get the lightest color from a list of hex values using javascript
I have a list of hex colors #000000 values. 我有一个十六进制颜色#000000值的列表。 How can I get the lightest color from a list of hex colors using javascript? 如何使用JavaScript从十六进制颜色列表中获得最浅的颜色?
Any help will be appreciated 任何帮助将不胜感激
According to this formula , you can calculate the luminance (brightness) of an RGB color like this: 根据此公式 ,您可以像这样计算RGB颜色的亮度(亮度):
L = 0.2126*R + 0.7152*G + 0.0722*B;
you can apply this on hex which won't be hard, then get the color with the maximum brightness: 您可以将其应用在不会很难的十六进制上,然后获得具有最大亮度的颜色:
function lum(hex) { var r = parseInt(hex.substr(1, 2), 16), g = parseInt(hex.substr(3, 2), 16), b = parseInt(hex.substr(5, 2), 16); return 0.2126*r + 0.7152*g + 0.0722*b;; } function lightest(colors) { var maxIndex = 0, maxLum = lum(colors[0]); for(var i = 1; i < colors.length; i++) { var iLum = lum(colors[i]); if(maxLum < iLum) { maxLum = iLum; maxIndex = i; } } return colors[maxIndex]; } console.log(lightest(["#ff0000", "#ffcd00", "#000000"]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.