如何以编程方式将色调添加到给定颜色?

发布于 2024-10-24 17:54:15 字数 817 浏览 6 评论 0原文

我尝试用 Java 实现我的颜色编辑器。它应该是一个简单的软件。用户将以十六进制 RGB 输入颜色,例如:0xFF00FF。我知道如何计算色相、色度、饱和度和亮度,但如何向给定颜色添加或减去色相值?这就是谜底。

有什么算法或公式可以使用吗?

现在我使用这种方法,但我认为结果与我使用Adobe Photoshop得到的结果不同。

public void addHue(float addHue) {
    float c = getChroma();
    addHue %= 6;
    if (addHue < 2) {
        float n = 1 - green;
        green = green + addHue * n;
    } else if (addHue < 4) {
        addHue -= 2;
        float n = 1 - blue;
        blue = blue + addHue * n;
    } else if (addHue < 6) {
        addHue -=4;
        float n = 1 - red;
        red = red + addHue * n;
    }
    if (green > 1) green = 1;
    else if (green < 0) green = 0;
    if (red > 1) red = 1;
    else if (red < 0) red = 0;
    if (blue > 1) blue = 1;
    else if (blue < 0) blue = 0;
}

I tried to implement a my color editor in Java. It should be a simple software. The user will input color in hexadecimal RGBs, for example: 0xFF00FF. I know how to calculate Hue, Chroma, Saturation and Lightness, but how to add or substract Hue value to this given color? This is the mystery.

Is there any algorithm or formula to use?

Now I use this method, but I think the result is different from what I got with Adobe Photoshop.

public void addHue(float addHue) {
    float c = getChroma();
    addHue %= 6;
    if (addHue < 2) {
        float n = 1 - green;
        green = green + addHue * n;
    } else if (addHue < 4) {
        addHue -= 2;
        float n = 1 - blue;
        blue = blue + addHue * n;
    } else if (addHue < 6) {
        addHue -=4;
        float n = 1 - red;
        red = red + addHue * n;
    }
    if (green > 1) green = 1;
    else if (green < 0) green = 0;
    if (red > 1) red = 1;
    else if (red < 0) red = 0;
    if (blue > 1) blue = 1;
    else if (blue < 0) blue = 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

柠栀 2024-10-31 17:54:15

java.awt.Color 类可以帮助解决这个问题。

The java.awt.Color class can help with this.

各自安好 2024-10-31 17:54:15

我真的不明白你试图通过将两种色调添加在一起来实现什么目的。

色调是一个角度,给出您所代表的颜色的“色调”。如果以度为单位考虑,色调将在 [0, 360[ 范围内。此外,值 45 和 405 将表示相同的色调。

当您想要操纵色调时,必须考虑到这一点。也就是说,如果您想要计算两个值的平均值,例如 355 和 5,正确的结果可能是 0(画一个圆并取这两个值之间的最小半角)或 180(最大半角)。


我在 Android(没有颜色操作实用程序)上的做法是创建我自己的颜色类,用于存储颜色的两种表示形式(rgb 和 hsv)。

然后我有允许设置 RGB 或设置 HSV 的方法。添加相同的色彩空间是微不足道的。然后,每次调用这些方法时,我都会分别调用 updateHSV 或 updateRGB 方法来根据新值计算颜色分量。

class Color {
  float[] hsv;
  float[] rgb;

  public void setRgb(float[] rgb) {
    System.arraycopy(rgb, 0, this.rgb, 0, 3);
    computeHsvFromRgb();
  }

  public void setHsv(float[] hsv) {
    System.arraycopy(hsv, 0, this.hsv, 0, 3);
    computeRgbFromHsv();
  }

  // ...
}

对于颜色转换示例代码:

I don't really understand what you try to achieve by adding two hues together.

Hue is an angle giving the "tint" of the color you are representing. If you think in degrees, the hue will be in the [0, 360[ range. Also, values of 45 and 405 will be representing the same hue.

You have to take this into account when you want to manipulate hues. That is, is you want to average two values, for instance 355 and 5, the correct result could be 0 (draw a circle and take the smallest half angle between those two values) or 180 (the largest half angle).


How I did it on Android (which does not have the Color manipulation utilities) is to create my own color class that stores both representations of a color (rgb and hsv).

Then I have methods that allow to set RGB or set HSV. Adding in the same color space is trivial. Then each time these methods are called, I respectively call an updateHSV or updateRGB method that computes the color components from the new values.

class Color {
  float[] hsv;
  float[] rgb;

  public void setRgb(float[] rgb) {
    System.arraycopy(rgb, 0, this.rgb, 0, 3);
    computeHsvFromRgb();
  }

  public void setHsv(float[] hsv) {
    System.arraycopy(hsv, 0, this.hsv, 0, 3);
    computeRgbFromHsv();
  }

  // ...
}

For color convertion sample code:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文