本文介绍如何将 HSB 颜色模式转换为 RGB 颜色模式。

关于这两种模式的介绍,可以参考 Colorfizer 。该网站也提供了在线的转换工具,可以用来验证结果。

HSB转换为RGB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public static double[] newRgb2hsb(int red, int green, int blue) {

double b, delta, g, max, min, r;

double hue, saturation, luminosity;

r = (double) red / MaxRGB;
g = (double) green / MaxRGB;
b = (double) blue / MaxRGB;
max = Math.max(r, Math.max(g, b));
min = Math.min(r, Math.min(g, b));

hue = 0.0;
saturation = 0.0;
luminosity = max;

delta = max - min;
if (delta == 0.0) {
return new double[]{hue, saturation, luminosity};
}

if (max != 0) {
saturation = delta / max;
}
if (r == max)
hue = 60 * (((g - b) / delta) % 6);
else if (g == max)
hue = 60 * ((b - r) / delta + 2);
else
hue = 60 * ((r - g) + 4);

return new double[]{hue, saturation, luminosity};
}
TAGS