rgb与hsi的相互转换代码java
时间: 2024-06-10 21:08:34 浏览: 78
c++实现RGB与HSI互相转换.docx
5星 · 资源好评率100%
RGB与HSI的相互转换代码如下所示:
```java
public class RGBtoHSIConverter {
public static void main(String[] args) {
int R = 100;
int G = 50;
int B = 150;
double H, S, I;
// RGB转HSI
double r = R / 255.0;
double g = G / 255.0;
double b = B / 255.0;
double min = Math.min(Math.min(r, g), b);
double max = Math.max(Math.max(r, g), b);
double delta = max - min;
if (delta == 0) {
H = 0;
} else if (max == r) {
H = (60 * ((g - b) / delta) + 360) % 360;
} else if (max == g) {
H = (60 * ((b - r) / delta) + 120) % 360;
} else {
H = (60 * ((r - g) / delta) + 240) % 360;
}
if (max == 0) {
S = 0;
} else {
S = 1 - min / max;
}
I = (r + g + b) / 3;
System.out.println("HSI: (" + H + ", " + S + ", " + I + ")");
// HSI转RGB
double r2, g2, b2;
if (H < 120) {
b2 = I * (1 - S);
r2 = I * (1 + (S * Math.cos(Math.toRadians(H)) / Math.cos(Math.toRadians(60 - H))));
g2 = 3 * I - (r2 + b2);
} else if (H < 240) {
H = H - 120;
r2 = I * (1 - S);
g2 = I * (1 + (S * Math.cos(Math.toRadians(H)) / Math.cos(Math.toRadians(60 - H))));
b2 = 3 * I - (r2 + g2);
} else {
H = H - 240;
g2 = I * (1 - S);
b2 = I * (1 + (S * Math.cos(Math.toRadians(H)) / Math.cos(Math.toRadians(60 - H))));
r2 = 3 * I - (g2 + b2);
}
int R2 = (int) (r2 * 255);
int G2 = (int) (g2 * 255);
int B2 = (int) (b2 * 255);
System.out.println("RGB: (" + R2 + ", " + G2 + ", " + B2 + ")");
}
}
```
阅读全文