color-spacecieluv

Determine that a Luv Color is non-imaginary


Suppose I have a triple of coordinates in the Luv color space. What is the best way to determine that these correspond to a real color?


Solution

  • Assuming your L* is in domain [0, 100], you can construct the boundaries of the Visible Spectrum and then determine if your CIE L*u*v* coordinates are within it.

    import colour
    import numpy as np
    
    
    def generate_square_waves(samples):
        square_waves = []
        square_waves_basis = np.tril(np.ones((samples, samples)))[0:-1, :]
        for i in range(samples):
            square_waves.append(np.roll(square_waves_basis, i))
        return np.vstack((np.zeros(samples), np.vstack(square_waves),
                        np.ones(samples)))
    
    
    def XYZ_outer_surface(samples):
        XYZ = []
        wavelengths = np.linspace(colour.DEFAULT_SPECTRAL_SHAPE.start,
                                colour.DEFAULT_SPECTRAL_SHAPE.end, samples)
    
        for wave in generate_square_waves(samples):
            spd = colour.SpectralPowerDistribution(
                wave, wavelengths, interpolator=colour.LinearInterpolator).align(
                    colour.DEFAULT_SPECTRAL_SHAPE)
            XYZ.append(colour.spectral_to_XYZ(spd))
    
        return np.array(XYZ).reshape(len(XYZ), -1, 3)
    
    mesh = XYZ_outer_surface(43).reshape((-1, 3))
    
    E = colour.ILLUMINANTS['CIE 1931 2 Degree Standard Observer']['E']
    
    XYZ1 = colour.Luv_to_XYZ([50, 50, 50], E)
    print(colour.is_within_mesh_volume(XYZ1, mesh))
    # True
    
    XYZ2 = colour.Luv_to_XYZ([50, 250, -250], E)
    print(colour.is_within_mesh_volume(XYZ2, mesh))
    # False
    

    I would highly recommend caching the mesh as it is quite heavy to compute.