#include <stdio.h>
#include <inttypes.h>
#include <math.h>

int16_t calculate_red(int16_t kelvin_temperature) {
	if (kelvin_temperature <= 6600) {
		return 255;
	} else {
		double res = pow(kelvin_temperature / 100 - 60, -0.1332047592) * 329.698727446;
		if (res > 255) {
			return 255;
		} else if (res < 0) {
			return 0;
		}
		return res;
	}
}

int16_t calculate_green(int16_t kelvin_temperature) {
	double res;
	if (kelvin_temperature <= 6600) {
		res = log(kelvin_temperature / 100) * 99.4708025861 - 161.1195681661;
	} else {
		res = pow(kelvin_temperature / 100 - 60, -0.0755148492) * 288.1221695283;
	}
	if (res > 255) {
		return 255;
	} else if (res < 0) {
		return 0;
	}
	return res;
}

int16_t calculate_blue(int16_t kelvin_temperature) {
	if (kelvin_temperature <= 1900) {
		return 0;
	} else if (kelvin_temperature >= 6600) {
		return 255;
	} else {
		double res = log(kelvin_temperature / 100 - 10) * 138.5177312231 - 305.0447927307;
		if (res > 255) {
			return 255;
		} else if (res < 0) {
			return 0;
		}
		return res;
	}
}

int main(int argc, char **argv) {
	if (argc < 2) {
		fputs("Specify the temperature in Kelvin whose color you want to calculate.\n", stderr);
		return 1;
	}
	int16_t k;
	if (argv[1][0] == '-' || sscanf(argv[1], "%" SCNd16, &k) != 1) {
		fputs("The temperature in Kelvin must be a positive integer.\n", stderr);
		return 1;
	}
	int r = calculate_red(k);
	int g = calculate_green(k);
	int b = calculate_blue(k);
	printf("#%02X%02X%02X\n", r, g, b);
	return 0;
}

Taken from Tanner Helland & Mitchell Charity: