简体   繁体   English

如何使用javascript从十六进制值列表中获取最浅的颜色

[英]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.

相关问题 如何在JavaScript中获取渐变颜色的十六进制代码(基于值的3种颜色) - How to get hex code of the gradient color (3 colors based on values) in JavaScript 如何使用javascript / jQuery增加/减少十六进制颜色值 - How to increment / decrement hex color values with javascript / jQuery 使用 javascript 从 rbg 值转换为十六进制代码后,将十六进制颜色显示为文本框的背景颜色 - Dispaly hex color as background color of textbox after conversion from rbg value to hex code using javascript 如何从javascript中的css颜色名称获取rgb(或十六进制)代码? - How can I get rgb (or hex) code from css color-name in javascript? 如何使用 javascript 将 RGBA 转换为十六进制颜色代码 - How to convert RGBA to Hex color code using javascript 如何使用JavaScript中的循环获取十六进制颜色数字的完整列表 - How to get the complete list of hexadecimal color numbers using a loop in javascript 如何从css获取十六进制颜色代码并自动显示在div中? - How to get hex color code from css and automatically display in div? 使用javascript或php从图像获取所有十六进制值 - Getting all HEX values from image using javascript or php Javascript 六角换色器 using.toString() - Javascript Hex color changer using .toString() 数组中的随机JavaScript十六进制颜色 - Random javascript HEX color from array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM