描述
Converts the color index B-V to RGB model.
例子
//package net.doc21;
public class Main {
public static void main(String[] argv) throws Exception {
float bv = 2.45678f;
System.out.println(java.util.Arrays.toString(BVtoRGB(bv)));
}/* w w w.21d oc . n e t*/
/**
* Converts the color index B-V to RGB model.
* See http://stackoverflow.com/questions/21977786/star-b-v-color-index-to-apparent-rgb-color
* @param bv The B-V coor index.
* @return
*/
public static float[] BVtoRGB(float bv) {
double t = 4600 * ((1 / ((0.92 * bv) + 1.7)) + (1 / ((0.92 * bv) + 0.62)));
// t to xyY
double x = 0, y = 0;
if (t >= 1667 && t <= 4000) {
x = ((-0.2661239 * Math.pow(10, 9)) / Math.pow(t, 3))
+ ((-0.2343580 * Math.pow(10, 6)) / Math.pow(t, 2))
+ ((0.8776956 * Math.pow(10, 3)) / t) + 0.179910;
} else if (t > 4000 && t <= 25000) {
x = ((-3.0258469 * Math.pow(10, 9)) / Math.pow(t, 3))
+ ((2.1070379 * Math.pow(10, 6)) / Math.pow(t, 2))
+ ((0.2226347 * Math.pow(10, 3)) / t) + 0.240390;
}
if (t >= 1667 && t <= 2222) {
y = -1.1063814 * Math.pow(x, 3) - 1.34811020 * Math.pow(x, 2)
+ 2.18555832 * x - 0.20219683;
} else if (t > 2222 && t <= 4000) {
y = -0.9549476 * Math.pow(x, 3) - 1.37418593 * Math.pow(x, 2)
+ 2.09137015 * x - 0.16748867;
} else if (t > 4000 && t <= 25000) {
y = 3.0817580 * Math.pow(x, 3) - 5.87338670 * Math.pow(x, 2)
+ 3.75112997 * x - 0.37001483;
}
// xyY to XYZ, Y = 1
double Y = (y == 0) ? 0 : 1;
double X = (y == 0) ? 0 : (x * Y) / y;
double Z = (y == 0) ? 0 : ((1 - x - y) * Y) / y;
float[] cc = new float[4];
cc[0] = correctGamma(3.2406 * X - 1.5372 * Y - 0.4986 * Z);
cc[1] = correctGamma(-0.9689 * X + 1.8758 * Y + 0.0415 * Z);
cc[2] = correctGamma(0.0557 * X - 0.2040 * Y + 1.0570 * Z);
float max = Math.max(1, Math.max(cc[2], Math.max(cc[0], cc[1])));
cc[0] = Math.max(cc[0] / max, 0f);
cc[1] = Math.max(cc[1] / max, 0f);
cc[2] = Math.max(cc[2] / max, 0f);
return cc;
}
private static float correctGamma(double clinear) {
float result;
if (clinear <= 0.0031308) {
result = 12.92f * (float) clinear;
} else {
// use 0.05 for pale colors, 0.5 for vivid colors
float a = 0.5f;
result = (float) ((1 + a) * Math.pow(clinear, 1 / 2.4f) - a);
}
return result;
}
}