Convert from color temperature to RGB (sRGB)

I wrote it halfway, but the HTML table doesn't work well, so see the Hatena blog for details. [Addition] I wrote it! This article. → Convert from color temperature to RGB (sRGB) (completed version)

What are you doing?

Have you ever seen this? Implement the code to find the sRGB value corresponding to the color temperature.

daylight.png

Outline of processing

  1. Color temperature range check
  2. Color temperature → xy chromaticity coordinates
  3. xyz → linear sRGB
  4. Normalize linear sRGB
  5. Linear sRGB → Non-linear sRGB
  6. Quantization of non-linear sRGB (integer from 0 to 255)
  7. Quantization sRGB → 0xAARRGGBB

Java as well as code

Because you too (abbreviated below)

	public static int daylight(double tcp) {
		double x, y, z, r, g, b;
		if (tcp < 4000.0) {
			throw new IllegalArgumentException();
		}
		else if (tcp <= 7000.0) {
			x = -4.6070E9 / (tcp * tcp * tcp) + 2.9678E6 / (tcp * tcp) + 0.09911E3 / tcp + 0.244063;
		}
		else if (tcp <= 25000.0) {
			x = -2.0064E9 / (tcp * tcp * tcp) + 1.9018E6 / (tcp * tcp) + 0.24748E3 / tcp + 0.237040;
		}
		else {
			throw new IllegalArgumentException();
		}
		y = -3.000 * (x * x) + 2.870 * x - 0.275;
		z = 1.0 - x - y;
		
		r = 3.2406 * x - 1.5372 * y - 0.4986 * z;
		g = -0.9689 * x + 1.8758 * y + 0.0415 * z;
		b = 0.0557 * x - 0.2040 * y + 1.0570 * z;
		
//		double maxRgb = Math.max(Math.max(r, g), b);
//		r /= maxRgb;
//		g /= maxRgb;
//		b /= maxRgb;
//		r = (r <= 0.0031308) ? 12.92 * r : 1.055 * Math.pow(r, 1.0 / 2.4) - 0.055;
//		g = (g <= 0.0031308) ? 12.92 * g : 1.055 * Math.pow(g, 1.0 / 2.4) - 0.055;
//		b = (b <= 0.0031308) ? 12.92 * b : 1.055 * Math.pow(b, 1.0 / 2.4) - 0.055;
		
//The commented out part above omits the branch in this situation.
//You can do the following
		
		double maxRgb = Math.max(Math.max(r, g), b);		
		r = 1.055 * Math.pow(r / maxRgb, 1.0 / 2.4) - 0.055;
		g = 1.055 * Math.pow(g / maxRgb, 1.0 / 2.4) - 0.055;
		b = 1.055 * Math.pow(b / maxRgb, 1.0 / 2.4) - 0.055;
		
		int rr = (int) (255.0 * r + 0.5);
		int gg = (int) (255.0 * g + 0.5);
		int bb = (int) (255.0 * b + 0.5);
		return 0xFF000000 | (rr << 16) | (gg << 8) | bb;
	}

Recommended Posts

Convert from color temperature to RGB (sRGB)
[Rails] How to convert from erb to haml
Convert from java UTC time to JST time
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
Convert from C String pointer to Swift String type
I tried to summarize Android development, RGB, hexadecimal number, color implementation from resource files
Launch Docker from Java to convert Office documents to PDF
Convert Java enum enums and JSON to and from Jackson
Android development, brightness adjustment in HSV from RGB color,
[Urgent recruitment] I can't convert from java.util.date to java.sql.date ...
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Migrate from JUnit 4 to JUnit 5
From Java to Ruby !!
[Java] How to convert a character string from String type to byte type