有没有办法判断 HTML 十六进制颜色是浅色还是深色
如果行的背景颜色为浅色,我希望 html 页面上的字体颜色更改为黑色;如果背景为白色,我希望将 html 页面上的字体颜色更改为黑色,
我在页面中使用 jsp。 有没有办法说这样的话
如果 color < ##0686FF 然后 fontcolour = #000000 例如
编辑:寻找 scriptlet 或 javascript
I want font colour on my html page to change to black if the background colour of the row is light and black if the background is white
I'm using jsp in my page. is there a way to say something like this
if colour < ##0686FF then fontcolour = #000000 for example
edit: looking for a scriptlet or javascript
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此解决方案使用
java.awt.Color
类来派生颜色的亮度值,并使用该值来确定应使用哪种背景颜色。编辑:此解决方案与其他一些解决方案不同,因为其他解决方案会将一些明亮的颜色视为深色,例如。原色红色 (#FF0000)。而此解决方案将认为原色红色是您可以拥有的最亮的颜色之一。我想这取决于你的喜好。您想读黑底红字还是白底红字?
HSB 代表色相(Hue)、饱和度(Saturation)、亮度(Brightness)——亮度也称为光度。颜色类别值介于 1 和 0 之间。因此,0.5 亮度是最亮颜色和最暗颜色之间的中间点。
色调、饱和度和亮度之间的相互作用比红色、蓝色和绿色稍微复杂一些。使用此工具尝试不同的颜色并找出 RGB 和 HSB 之间的关系
This solution uses the
java.awt.Color
class to derive the brightness value of the colour and use that to determine which background colour should be used.edit: This solution is differs from some of the other solutions because the other solutions will consider some bright colours to be dark eg. primary red (#FF0000). Whereas this solution, will consider primary red to be one of the brightest colours you can have. I suppose it depends on your preferences. Do you want to read red on black or red on white?
HSB stands for Hue, Saturation, Brightness -- brightness is also known as luminosity. With the Color class values are between 1 and 0. Ergo, 0.5 brightness is the the halfway point between the brightest colours and the darkest colours).
The interplay between hue, saturation and brightness are a little more complex than red, blue and green. Use this tool experiment with different colours and find the relationships between RGB and HSB
我们所说的“颜色”通常是指 24 位 RGB 颜色:1 字节(8 位)表示红、绿、蓝。即每个通道的值范围为0-255,即十六进制显示为0x00 至0xff。
白色表示所有通道已满:#FFFFFF,黑色表示所有通道关闭:#000000。显然,较浅的颜色意味着通道中的值较高,较深的颜色意味着通道中的值较低。
具体如何选择算法取决于您,简单的一个是:
编辑:询问者要求提供更完整的示例,以便他/她可以有更好的开始,所以这里是:
它产生输出:
并展示了将 #aabbcc 格式的颜色拆分为 RGB 通道、稍后加入它们(如果需要)等的技术。
By "color" we usually mean 24-bit RGB color: 1 byte (8 bits) for red, green, blue. That is, every channel has value from 0-255, or 0x00 to 0xff in hexadecimal display.
White color is all channels at full: #FFFFFF, black is all channels turned off: #000000. Obviously, lighter color means higher values in channels, darker color means lower values in channels.
How exactly you choose your algorithm is up to you, simple one would be:
Edit: asker asks for more complete example, so he/she can have better start, so here it is:
It produces output:
And shows technique of splitting color in format #aabbcc into rgb channels, joining them later (if needed), etc.
更自然的方法可能是使用 HSL 色彩空间。您可以使用第三个分量(“亮度”)来计算颜色的亮度。这适用于大多数目的。
谷歌搜索有大量描述。基本上,您采用具有最高值的颜色分量(假设它是红色)和具有最低值的颜色分量(假设它是绿色)。在本例中:
假设您将颜色提取为 0 到 255 之间的值。您将获得 0 到 1 之间的值。浅色可以是亮度高于某个任意值(例如 0.6)的任何颜色。
A more natural approach might be using the HSL colorspace. You can use the third component ("lightness") to figure out how light a colour is. That will work for most purposes.
Plenty of descriptions out there by googling. Basically you take the colour component with the highest value (let's say it's red) and the one with the lowest (say, green). In this case:
Assuming you extracted your colours as values from 0 to 255. You'll get a value between 0 and 1. A light colour could be any colour with a lightness above a certain arbitrary value (for instance, 0.6).
http://www.careerbless.com/services/css/csstooltipcreator 中使用的颜色选择器。 php
效果很好
The color picker used in http://www.careerbless.com/services/css/csstooltipcreator.php
works good
这是上面 Kiran 在 PHP 中的答案,这对我来说非常有用。谢谢基兰。
用法(使用 maliayas 的 adjustmentBrightness() 函数):
Here is Kiran's answer from above in PHP, which works great for me. Thanks Kiran.
Usage (using maliayas's adjustBrightness() function):