有没有办法判断 HTML 十六进制颜色是浅色还是深色

发布于 2024-10-11 12:14:15 字数 201 浏览 2 评论 0原文

如果行的背景颜色为浅色,我希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

桃扇骨 2024-10-18 12:14:15

此解决方案使用 java.awt.Color 类来派生颜色的亮度值,并使用该值来确定应使用哪种背景颜色。

编辑:此解决方案与其他一些解决方案不同,因为其他解决方案会将一些明亮的颜色视为深色,例如。原色红色 (#FF0000)。而此解决方案将认为原色红色是您可以拥有的最亮的颜色之一。我想这取决于你的喜好。您想读黑底红字还是白底红字?

String fontColor = "#0cf356";

// remove hash character from string
String rawFontColor = fontColor.substring(1,fontColor.length());

// convert hex string to int
int rgb = Integer.parseInt(rawFontColor, 16);

Color c = new Color(rgb);

float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);

float brightness = hsb[2];

if (brightness < 0.5) {
   // use a bright background
} else {
   // use a dark background
}

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?

String fontColor = "#0cf356";

// remove hash character from string
String rawFontColor = fontColor.substring(1,fontColor.length());

// convert hex string to int
int rgb = Integer.parseInt(rawFontColor, 16);

Color c = new Color(rgb);

float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);

float brightness = hsb[2];

if (brightness < 0.5) {
   // use a bright background
} else {
   // use a dark background
}

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

指尖上得阳光 2024-10-18 12:14:15

我们所说的“颜色”通常是指 24 位 RGB 颜色:1 字节(8 位)表示红、绿、蓝。即每个通道的值范围为0-255,即十六进制显示为0x00 至0xff。

白色表示所有通道已满:#FFFFFF,黑色表示所有通道关闭:#000000。显然,较浅的颜色意味着通道中的值较高,较深的颜色意味着通道中的值较低。

具体如何选择算法取决于您,简单的一个是:

//pseudo-code
if (red + green + blue <= (0xff * 3) / 2) //half-down, half-up
  fontcolor = white;
else
  fontcolor = black;

编辑:询问者要求提供更完整的示例,以便他/她可以有更好的开始,所以这里是:

public static void main(String[] args) throws IOException {

String value =
       // new Scanner(System.in).nextLine(); //from input
        "#112233"; //from constant
int red = Integer.parseInt(value.substring(1, 1 + 2), 16);
int green = Integer.parseInt(value.substring(3, 3 + 2), 16);
int blue = Integer.parseInt(value.substring(5, 5 + 2), 16);

System.out.println("red = " + Integer.toHexString(red)
        + ", green = " + Integer.toHexString(green)
        + ", blue = " + Integer.toHexString(blue));

if (red + green + blue <= 0xff * 3 / 2)
    System.out.println("using white color #ffffff");
else
    System.out.println("using black color #000000");

String colorBackToString = "#" + Integer.toHexString(red) +
        Integer.toHexString(green) +
        Integer.toHexString(blue);
System.out.println("color was " + colorBackToString);
}

它产生输出:

red = 11, green = 22, blue = 33
using white color #ffffff
color was #112233

并展示了将 #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:

//pseudo-code
if (red + green + blue <= (0xff * 3) / 2) //half-down, half-up
  fontcolor = white;
else
  fontcolor = black;

Edit: asker asks for more complete example, so he/she can have better start, so here it is:

public static void main(String[] args) throws IOException {

String value =
       // new Scanner(System.in).nextLine(); //from input
        "#112233"; //from constant
int red = Integer.parseInt(value.substring(1, 1 + 2), 16);
int green = Integer.parseInt(value.substring(3, 3 + 2), 16);
int blue = Integer.parseInt(value.substring(5, 5 + 2), 16);

System.out.println("red = " + Integer.toHexString(red)
        + ", green = " + Integer.toHexString(green)
        + ", blue = " + Integer.toHexString(blue));

if (red + green + blue <= 0xff * 3 / 2)
    System.out.println("using white color #ffffff");
else
    System.out.println("using black color #000000");

String colorBackToString = "#" + Integer.toHexString(red) +
        Integer.toHexString(green) +
        Integer.toHexString(blue);
System.out.println("color was " + colorBackToString);
}

It produces output:

red = 11, green = 22, blue = 33
using white color #ffffff
color was #112233

And shows technique of splitting color in format #aabbcc into rgb channels, joining them later (if needed), etc.

ま柒月 2024-10-18 12:14:15

更自然的方法可能是使用 HSL 色彩空间。您可以使用第三个分量(“亮度”)来计算颜色的亮度。这适用于大多数目的。

谷歌搜索有大量描述。基本上,您采用具有最高值的颜色分量(假设它是红色)和具有最低值的颜色分量(假设它是绿色)。在本例中:

L = (red + green) / (255*2.0)

假设您将颜色提取为 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:

L = (red + green) / (255*2.0)

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).

真心难拥有 2024-10-18 12:14:15
function isTooLightYIQ(hexcolor)
{
      var r = parseInt(hexcolor.substr(0,2),16);
      var g = parseInt(hexcolor.substr(2,2),16);
      var b = parseInt(hexcolor.substr(4,2),16);
      var yiq = ((r*299)+(g*587)+(b*114))/1000;
      return yiq >= 128;
}

http://www.careerbless.com/services/css/csstooltipcreator 中使用的颜色选择器。 php
效果很好

function isTooLightYIQ(hexcolor)
{
      var r = parseInt(hexcolor.substr(0,2),16);
      var g = parseInt(hexcolor.substr(2,2),16);
      var b = parseInt(hexcolor.substr(4,2),16);
      var yiq = ((r*299)+(g*587)+(b*114))/1000;
      return yiq >= 128;
}

The color picker used in http://www.careerbless.com/services/css/csstooltipcreator.php
works good

水水月牙 2024-10-18 12:14:15

这是上面 Kiran 在 PHP 中的答案,这对我来说非常有用。谢谢基兰。

function isTooLightYIQ($hexcolor)
{
    $hexcolor = ltrim($hexcolor, "#");
    $r = base_convert(substr($hexcolor, 0, 2), 16, 10); 
    $g = base_convert(substr($hexcolor, 2, 2), 16, 10); 
    $b = base_convert(substr($hexcolor, 4, 2), 16, 10); 
    $yiq = (($r * 299) + ($g * 587) + ($b * 114)) / 1000;
    return $yiq >= 128;
}

用法(使用 maliayas 的 adjustmentBrightness() 函数):

$adjustPercent = -0.45;  //  45% darker
$darkHexColor = (isTooLightYIQ($hexColor)) ? adjustBrightness($hexColor, $adjustPercent) : $HexColor;

Here is Kiran's answer from above in PHP, which works great for me. Thanks Kiran.

function isTooLightYIQ($hexcolor)
{
    $hexcolor = ltrim($hexcolor, "#");
    $r = base_convert(substr($hexcolor, 0, 2), 16, 10); 
    $g = base_convert(substr($hexcolor, 2, 2), 16, 10); 
    $b = base_convert(substr($hexcolor, 4, 2), 16, 10); 
    $yiq = (($r * 299) + ($g * 587) + ($b * 114)) / 1000;
    return $yiq >= 128;
}

Usage (using maliayas's adjustBrightness() function):

$adjustPercent = -0.45;  //  45% darker
$darkHexColor = (isTooLightYIQ($hexColor)) ? adjustBrightness($hexColor, $adjustPercent) : $HexColor;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文