java 以编程方式找到颜色的补充?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3054873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:07:41  来源:igfitidea点击:

Programmatically find complement of colors?

javacolors

提问by Mohit Deshpande

Is there a way to find the complement of a color given its RGB values? Or can it only be found for certain colors? How would someone go about doing this in Java?

有没有办法找到给定 RGB 值的颜色的补色?还是只能找到某些颜色?有人会如何在 Java 中做到这一点?

回答by Poypoyan

This is what I come up: (Conjecture)

这是我想出来的:(猜想)

Let r, g, and bbe RGB components of the original color.

rgb是原始颜色的 RGB 分量。

Let r', g', and b'be RGB components of the complementary color.

r'g'、 和b'是互补色的 RGB 分量。

Then:

然后:

r' = max(r,b,g) + min(r,b,g) - r   
b' = max(r,b,g) + min(r,b,g) - b
g' = max(r,b,g) + min(r,b,g) - g

I see that this gives the same answer to what the websites (i.e. www.color-hex.com) give, but I will still prove it. :)

我看到这与网站(即 www.color-hex.com)给出的答案相同,但我仍然会证明这一点。:)

回答by Poypoyan

Just focusing on the 3D RGB cube. Here's a function that does a binary search in that cube to avoid having to figure out how to cast vectors in a direction and find the closest point on the cube boundary that intersects it.

只关注 3D RGB 立方体。这是一个在该立方体中进行二分搜索的函数,以避免必须弄清楚如何在一个方向上投射向量并找到与其相交的立方体边界上的最近点。

def farthestColorInRGBcubeFrom(color):
  def pathsAt(r, g, b):
    paths = [(r, g, b)]
    if r < 255:
        paths.append((int((r + 255)/2), g, b))
    if r > 0:
        paths.append((int(r/2), g, b))
    if g < 255:
        paths.append((r, int((g + 255)/2), b))
    if g > 0:
        paths.append((r, int(g/2), b))
    if b < 255:
        paths.append((r, g, int((b + 255)/2)))
    if b > 0:
        paths.append((r, g, int(b/2)))
    return paths                         
r = color.red();  g = color.green();  b = color.blue();
#naive guess:
r0 = 255 - r;  g0 = 255 - g;  b0 = 255 - b;
paths = pathsAt(r0, g0, b0)
maxPath = None
while paths != []:
    for p in paths:
        d = (r - p[0])**2 + (g - p[1])**2 + (b - p[0])**2
        if maxPath != None:
            if d > maxPath[0]:
                maxPath = (d, p)
        else:
            maxPath = (d, p)
    p = maxPath[1]
    paths = pathsAt(p[0], p[1], p[2])
c = maxPath[1]
return QColor(c[0], c[1], c[2], color.alpha())

回答by Edd

For a rough approximation, you can do this by converting RGB to HSL (Hue, Saturation, Lightness).

对于粗略的近似值,您可以通过将 RGB 转换为 HSL(色相、饱和度、亮度)来实现。

With the HSL value, shift the hue 180 degrees to get a color on the opposite of the color wheel from the original value.

使用 HSL 值,将色调移动 180 度以获得与原始值相反的色轮颜色。

Finally, convert back to RGB.

最后,转换回RGB。

I've written a JavaScript implementation using a hex value here - https://stackoverflow.com/a/37657940/4939630

我在这里使用十六进制值编写了一个 JavaScript 实现 - https://stackoverflow.com/a/37657940/4939630

For RGB, simply remove the hex to RGB and RGB to hex conversions.

对于 RGB,只需删除十六进制到 RGB 和 RGB 到十六进制的转换。

回答by tm1701

A more general and simple solution for finding the opposite color is:

找到相反颜色的更通用和简单的解决方案是:

private int getComplementaryColor( int color) {
    int R = color & 255;
    int G = (color >> 8) & 255;
    int B = (color >> 16) & 255;
    int A = (color >> 24) & 255;
    R = 255 - R;
    G = 255 - G;
    B = 255 - B;
    return R + (G << 8) + ( B << 16) + ( A << 24);
}

回答by Noakai Aronesty

No, Randy is correct! 127 and 128 are complementary pairs. Look, a complementary pair is a set of two colors that, when combined, form a grayscale color. 127 and 128 are both very close to grayscale colors themselves. Also, cyan and red arecomplementary.

不,兰迪是对的!127和128是互补对。看,互补对是一组两种颜色,当它们组合时,形成灰度颜色。127 和 128 都非常接近灰度颜色本身。此外,青色和红色互补的。

回答by JeffThompson

None of the answers above really give a way to find the complimentary color, so here's my version written in Processing:

上面的答案都没有真正提供一种找到互补颜色的方法,所以这是我用 Processing 编写的版本:

import java.awt.Color;

color initial = color(100, 0, 100);

void setup() {
  size(400, 400);  

  fill(initial);
  rect(0, 0, width/2, height);

  color opposite = getOppositeColor(initial);
  fill(opposite);
  rect(width/2, 0, width/2, height);
}

color getOppositeColor(color c) {
  float[] hsv = new float[3];
  Color.RGBtoHSB(c>>16&0xFF, c>>8&0xFF, c&0xFF, hsv);
  hsv[0] = (hsv[0] + 0.5) % 1.0;

  // black or white? return opposite
  if (hsv[2] == 0) return color(255);
  else if (hsv[2] == 1.0) return color(0);

  // low value? otherwise, adjust that too
  if (hsv[2] < 0.5) {
    hsv[2] = (hsv[2] + 0.5) % 1.0;
  }

  return Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]);
}

回答by Brill Pappin

You might find more information at: Programmatically choose high-contrast colors

您可能会在以下位置找到更多信息:以 编程方式选择高对比度颜色

public static int complimentaryColour(int colour) {
    return ~colour;
}

回答by Randy

it should just be math

它应该只是数学

the total of the r values of the 2 colors should be 255

2 种颜色的 r 值总和应为 255

the total of the g values of the 2 colors should be 255

2种颜色的g值总和应该是255

the total of the b values of the 2 colors should be 255

2 种颜色的 b 值总和应为 255