def wavelength_to_rgb(wavelength: float, gamma: float = 2.4) -> Tuple[int, int, int]: """ Converts a wavelength (in nanometres) to a gamma corrected RGB tuple with values [0, 255]. Returns white if the wavelength is outside the visible spectrum or any other error occurs. """ try: xyz = colour.wavelength_to_XYZ(wavelength) srgb = colour.XYZ_to_sRGB(xyz).clip(0, 1) gamma_corrected_rgb = 255 * srgb**(1 / gamma) return tuple(gamma_corrected_rgb) except ValueError: return 255, 255, 255
def wavelengthToHex(wavelength: float, gamma: float = 2.4): """ Converts a wavelength (in nanometres) to a gamma corrected RGB tuple with values [0, 255]. Returns white if the wavelength is outside the visible spectrum or any other error occurs. """ try: xyz = colour.wavelength_to_XYZ(wavelength) srgb = colour.XYZ_to_sRGB(xyz).clip(0, 1) gamma_corrected_rgb = 255 * srgb**(1 / gamma) chex = '#%02x%02x%02x' % (int( gamma_corrected_rgb[0]), int( gamma_corrected_rgb[1]), int(gamma_corrected_rgb[2])) return chex except ValueError: return 255, 255, 255
755: 0.450, 760: 0.462, 765: 0.465, 770: 0.448, 775: 0.432, 780: 0.421 } spd = colour.SpectralPowerDistribution('Sample', sample_spd_data) cmfs = colour.CMFS['CIE 1931 2 Degree Standard Observer'] illuminant = colour.ILLUMINANTS_RELATIVE_SPDS['A'] message_box(('Computing *CIE XYZ* tristimulus values for sample spectral ' 'power distribution and "CIE Standard Illuminant A".')) print(colour.spectral_to_XYZ(spd, cmfs, illuminant)) print('\n') message_box(('Computing "CIE Standard Illuminant A" chromaticity coordinates ' 'from its relative spectral power distribution.')) print(colour.XYZ_to_xy(colour.spectral_to_XYZ(illuminant, cmfs))) print('\n') message_box(('Computing *CIE XYZ* tristimulus values for a single given ' 'wavelength in nm.')) print( colour.wavelength_to_XYZ( 546.1, colour.CMFS['CIE 1931 2 Degree Standard Observer']))
print(colour.sd_to_XYZ(sd_sample, cmfs, illuminant)) print("\n") message_box( 'Computing "CIE Standard Illuminant A" chromaticity coordinates from its ' "spectral distribution.") print(colour.XYZ_to_xy(colour.sd_to_XYZ(illuminant, cmfs) / 100)) print("\n") message_box( "Computing *CIE XYZ* tristimulus values for a single given wavelength in " "nm.") print( colour.wavelength_to_XYZ( 546.1, colour.MSDS_CMFS["CIE 1931 2 Degree Standard Observer"])) message_box( "Computing *CIE XYZ* tristimulus values from given multi-spectral image " "with shape (4, 3, 6).") msds = np.array([ [ [ 0.01367208, 0.09127947, 0.01524376, 0.02810712, 0.19176012, 0.04299992, ], [
def wavelength_to_rgb_2(wavelength): XYZ = colour.wavelength_to_XYZ(wavelength) RGB = colour.XYZ_to_sRGB(XYZ) return torch.Tensor(RGB)
750: 0.436, 755: 0.450, 760: 0.462, 765: 0.465, 770: 0.448, 775: 0.432, 780: 0.421} spd = colour.SpectralPowerDistribution('Sample', sample_spd_data) cmfs = colour.CMFS['CIE 1931 2 Degree Standard Observer'] illuminant = colour.ILLUMINANTS_RELATIVE_SPDS['A'] message_box(('Computing *CIE XYZ* tristimulus values for sample spectral ' 'power distribution and "CIE Standard Illuminant A".')) print(colour.spectral_to_XYZ(spd, cmfs, illuminant)) print('\n') message_box(('Computing "CIE Standard Illuminant A" chromaticity coordinates ' 'from its relative spectral power distribution.')) print(colour.XYZ_to_xy(colour.spectral_to_XYZ(illuminant, cmfs))) print('\n') message_box(('Computing *CIE XYZ* tristimulus values for a single given ' 'wavelength in nm.')) print(colour.wavelength_to_XYZ( 546.1, colour.CMFS['CIE 1931 2 Degree Standard Observer']))