示例#1
0
def xyzFromRGB(red, green=None, blue=None):
	"""
	RGB转成xyz
	"""
	if isinstance(red, basestring):
		# assume a hex string is passed
		if red[0] == "#":
			colorString = red[1:]
		else:
			colorString = red
		red = int(colorString[0:2], 16)
		green = int(colorString[2:4], 16)
		blue = int(colorString[4:], 16)

	# We need to convert the RGB value to Yxy.
	redScale = float(red) / 255.0
	greenScale = float(green) / 255.0
	blueScale = float(blue) / 255.0
	colormodels.init(
		phosphor_red=colormodels.xyz_color(0.64843, 0.33086),
		phosphor_green=colormodels.xyz_color(0.4091, 0.518),
		phosphor_blue=colormodels.xyz_color(0.167, 0.04))
	xyz = colormodels.irgb_color(red, green, blue)
	xyz = colormodels.xyz_from_rgb(xyz)
	xyz = colormodels.xyz_normalize(xyz)
	return xyz
示例#2
0
def init(display_intensity=DEFAULT_DISPLAY_INTENSITY):
    '''Initialize the spectral sampling curves.'''
    # Expect that the table ranges from 360 to 830
    global start_wl_nm, end_wl_nm, delta_wl_nm
    table_size = len(_CIEXYZ_1931_table)
    start_wl_nm = 360
    end_wl_nm = 830
    delta_wl_nm = 1.0
    first = _CIEXYZ_1931_table[0][0]
    last = _CIEXYZ_1931_table[-1][0]
    assert (first == start_wl_nm
            ), 'Expecting first wavelength as %d but instead is %d' % (
                start_wl_nm, first)
    assert (
        last == end_wl_nm
    ), 'Expecting last wavelength as %d but instead is %d' % (end_wl_nm, last)
    assert (
        table_size == 471
    ), 'Expecting 471 wavelength, each 1 nm from 360 to 830 nm, instead table size is %d' % (
        table_size)
    # Assume that the color for the wl just before and after the table (359 and 831) are zero.
    # Also assume linear interpolation of the values for in-between nanometer wavelengths.
    # Construct arrays, with elements for each wavelength, as the xyz color,
    # and the change in color to the next largest nanometer.
    # We will add an (empty) entry for 359 nm and 831 nm.
    global _wavelengths, _xyz_colors, _xyz_deltas
    create_table_size = table_size + 2
    _wavelengths = numpy.empty((create_table_size), int)
    _xyz_colors = numpy.empty((create_table_size, 3))
    _xyz_deltas = numpy.empty((create_table_size, 3))
    # fill in first row as 359 nm with zero color
    _wavelengths[0] = start_wl_nm - 1
    _xyz_colors[0] = colormodels.xyz_color(0.0, 0.0, 0.0)
    # fill in last row as 831 nm with zero color
    _wavelengths[create_table_size - 1] = end_wl_nm + 1
    _xyz_colors[create_table_size - 1] = colormodels.xyz_color(0.0, 0.0, 0.0)
    # fill in the middle rows from the source data
    for i in xrange(0, len(_CIEXYZ_1931_table)):
        (wl, x, y, z) = _CIEXYZ_1931_table[i]
        _wavelengths[i + 1] = wl
        _xyz_colors[i + 1] = colormodels.xyz_color(x, y, z)
    # get the integrals of each curve
    integral = numpy.zeros(3)
    for i in xrange(0, create_table_size - 1):
        d_integral = 0.5 * (_xyz_colors[i] + _xyz_colors[i + 1]) * delta_wl_nm
        integral += d_integral
    # scale the sampling curves so that:
    #   A spectrum, constant with wavelength, with total intensity equal to the
    #   physical intensity of the monitor, will sample with Y = 1.0.
    # This scaling corresponds with that in colormodels, which assumes Y = 1.0 at full white.
    # Ideally, we would like the spectrum of the actual monitor display, at full white,
    #   to sample to Y = 1.0, not the constant with wavelength spectrum that is assumed here.
    num_wl = table_size
    scaling = num_wl / (integral[1] * display_intensity)
    _xyz_colors *= scaling
    # now calculate all the deltas
    for i in xrange(0, create_table_size - 1):
        _xyz_deltas[i] = _xyz_colors[i + 1] - _xyz_colors[i]
    _xyz_deltas[create_table_size - 1] = colormodels.xyz_color(0.0, 0.0, 0.0)
示例#3
0
 def test_uv_primes_inverse_1(self, verbose=False):
     ''' Test that uv_primes() and uv_primes_inverse() are inverses. '''
     # Test some random values.
     for i in range(20):
         x0 = 10.0 * random.random()
         y0 = 10.0 * random.random()
         z0 = 10.0 * random.random()
         xyz0 = colormodels.xyz_color(x0, y0, z0)
         self.check_uv_primes_inverse_1(xyz0, verbose)
     # Test black explicitly.
     xyz0 = colormodels.xyz_color(0.0, 0.0, 0.0)
     self.check_uv_primes_inverse_1(xyz0, verbose)
 def test_uv_primes_inverse_1(self, verbose=False):
     ''' Test that uv_primes() and uv_primes_inverse() are inverses. '''
     # Test some random values.
     for i in range (20):
         x0 = 10.0 * random.random()
         y0 = 10.0 * random.random()
         z0 = 10.0 * random.random()
         xyz0 = colormodels.xyz_color (x0, y0, z0)
         self.check_uv_primes_inverse_1(xyz0, verbose)
     # Test black explicitly.
     xyz0 = colormodels.xyz_color (0.0, 0.0, 0.0)
     self.check_uv_primes_inverse_1(xyz0, verbose)
示例#5
0
def test_xyz_lab (verbose=1):
    '''Test that lab_from_xyz() and xyz_from_lab() are inverses.'''

    def test_A (xyz0, tolerance=1.0e-10, verbose=1):
        '''Test that lab_from_xyz() and xyz_from_lab() are inverses.'''
        lab0 = colormodels.lab_from_xyz (xyz0)
        xyz1 = colormodels.xyz_from_lab (lab0)
        lab1 = colormodels.lab_from_xyz (xyz1)
        # check errors
        dlab = lab1 - lab0
        error_lab = math.sqrt (numpy.dot (dlab, dlab))
        dxyz = xyz1 - xyz0
        error_xyz = math.sqrt (numpy.dot (dxyz, dxyz))
        passed = (error_lab <= tolerance) and (error_xyz <= tolerance)
        if passed:
            status = 'pass'
        else:
            status = 'FAILED'
        msg = 'test_xyz_lab.test_A() : xyz0 = %s, lab(xyz0) = %s, xyz(lab(xyz0)) = %s, lab(xyz(lab(xyz0))) = %s, errors = (%g, %g), %s' % (
            str (xyz0), str (lab0), str (xyz1), str (lab1), error_lab, error_xyz, status)
        if verbose >= 1:
            print msg
        if not passed:
            pass
            raise ValueError, msg
        return passed

    num_passed = 0
    num_failed = 0

    for i in xrange (0, 100):
        x0 = 10.0 * random.random()
        y0 = 10.0 * random.random()
        z0 = 10.0 * random.random()
        xyz0 = colormodels.xyz_color (x0,y0,z0)
        passed = test_A (xyz0, tolerance=1.0e-10, verbose=verbose)
        if passed:
            num_passed += 1
        else:
            num_failed += 1

    # Test black explicitly
    xyz0 = colormodels.xyz_color (0.0, 0.0, 0.0)
    passed = test_A (xyz0, tolerance=1.0e-10, verbose=verbose)
    if passed:
        num_passed += 1
    else:
        num_failed += 1

    msg = 'test_xyz_lab() : %d tests passed, %d tests failed' % (
        num_passed, num_failed)
    print msg
示例#6
0
def test_xyz_lab(verbose=1):
    '''Test that lab_from_xyz() and xyz_from_lab() are inverses.'''
    def test_A(xyz0, tolerance=1.0e-10, verbose=1):
        '''Test that lab_from_xyz() and xyz_from_lab() are inverses.'''
        lab0 = colormodels.lab_from_xyz(xyz0)
        xyz1 = colormodels.xyz_from_lab(lab0)
        lab1 = colormodels.lab_from_xyz(xyz1)
        # check errors
        dlab = lab1 - lab0
        error_lab = math.sqrt(numpy.dot(dlab, dlab))
        dxyz = xyz1 - xyz0
        error_xyz = math.sqrt(numpy.dot(dxyz, dxyz))
        passed = (error_lab <= tolerance) and (error_xyz <= tolerance)
        if passed:
            status = 'pass'
        else:
            status = 'FAILED'
        msg = 'test_xyz_lab.test_A() : xyz0 = %s, lab(xyz0) = %s, xyz(lab(xyz0)) = %s, lab(xyz(lab(xyz0))) = %s, errors = (%g, %g), %s' % (
            str(xyz0), str(lab0), str(xyz1), str(lab1), error_lab, error_xyz,
            status)
        if verbose >= 1:
            print(msg)
        if not passed:
            pass
            raise ValueError(msg)
        return passed

    num_passed = 0
    num_failed = 0

    for i in xrange(0, 100):
        x0 = 10.0 * random.random()
        y0 = 10.0 * random.random()
        z0 = 10.0 * random.random()
        xyz0 = colormodels.xyz_color(x0, y0, z0)
        passed = test_A(xyz0, tolerance=1.0e-10, verbose=verbose)
        if passed:
            num_passed += 1
        else:
            num_failed += 1

    # Test black explicitly
    xyz0 = colormodels.xyz_color(0.0, 0.0, 0.0)
    passed = test_A(xyz0, tolerance=1.0e-10, verbose=verbose)
    if passed:
        num_passed += 1
    else:
        num_failed += 1

    msg = 'test_xyz_lab() : %d tests passed, %d tests failed' % (num_passed,
                                                                 num_failed)
    print(msg)
示例#7
0
文件: ciexyz.py 项目: Stanpol/ColorPy
def init (display_intensity = DEFAULT_DISPLAY_INTENSITY):
    '''Initialize the spectral sampling curves.'''
    # Expect that the table ranges from 360 to 830
    global start_wl_nm, end_wl_nm, delta_wl_nm
    table_size = len (_CIEXYZ_1931_table)
    start_wl_nm = 360
    end_wl_nm   = 830
    delta_wl_nm = 1.0
    first = _CIEXYZ_1931_table [0][0]
    last  = _CIEXYZ_1931_table [-1][0]
    assert (first == start_wl_nm), 'Expecting first wavelength as %d but instead is %d' % (start_wl_nm, first)
    assert (last == end_wl_nm), 'Expecting last wavelength as %d but instead is %d' % (end_wl_nm, last)
    assert (table_size == 471), 'Expecting 471 wavelength, each 1 nm from 360 to 830 nm, instead table size is %d' % (table_size)
    # Assume that the color for the wl just before and after the table (359 and 831) are zero.
    # Also assume linear interpolation of the values for in-between nanometer wavelengths.
    # Construct arrays, with elements for each wavelength, as the xyz color,
    # and the change in color to the next largest nanometer.
    # We will add an (empty) entry for 359 nm and 831 nm.
    global _wavelengths, _xyz_colors, _xyz_deltas
    create_table_size = table_size + 2
    _wavelengths = numpy.empty ((create_table_size), int)
    _xyz_colors = numpy.empty ((create_table_size, 3))
    _xyz_deltas = numpy.empty ((create_table_size, 3))
    # fill in first row as 359 nm with zero color
    _wavelengths [0] = start_wl_nm - 1
    _xyz_colors  [0] = colormodels.xyz_color (0.0, 0.0, 0.0)
    # fill in last row as 831 nm with zero color
    _wavelengths [create_table_size-1] = end_wl_nm + 1
    _xyz_colors  [create_table_size-1] = colormodels.xyz_color (0.0, 0.0, 0.0)
    # fill in the middle rows from the source data
    for i in xrange (0, len (_CIEXYZ_1931_table)):
        (wl,x,y,z) = _CIEXYZ_1931_table [i]
        _wavelengths [i+1] = wl
        _xyz_colors  [i+1] = colormodels.xyz_color (x,y,z)
    # get the integrals of each curve
    integral = numpy.zeros (3)
    for i in xrange (0, create_table_size-1):
        d_integral = 0.5 * (_xyz_colors [i] + _xyz_colors [i+1]) * delta_wl_nm
        integral += d_integral
    # scale the sampling curves so that:
    #   A spectrum, constant with wavelength, with total intensity equal to the
    #   physical intensity of the monitor, will sample with Y = 1.0.
    # This scaling corresponds with that in colormodels, which assumes Y = 1.0 at full white.
    # Ideally, we would like the spectrum of the actual monitor display, at full white,
    #   to sample to Y = 1.0, not the constant with wavelength spectrum that is assumed here.
    num_wl = table_size
    scaling = num_wl / (integral [1] * display_intensity)
    _xyz_colors *= scaling
    # now calculate all the deltas
    for i in xrange (0, create_table_size-1):
        _xyz_deltas [i] = _xyz_colors [i+1] - _xyz_colors [i]
    _xyz_deltas [create_table_size-1] = colormodels.xyz_color (0.0, 0.0, 0.0)
示例#8
0
 def test_xyz_lab(self, verbose=False):
     '''Test that lab_from_xyz() and xyz_from_lab() are inverses.'''
     for i in range(100):
         x0 = 10.0 * random.random()
         y0 = 10.0 * random.random()
         z0 = 10.0 * random.random()
         xyz0 = colormodels.xyz_color(x0, y0, z0)
         self.check_xyz_lab(xyz0, verbose)
 def test_xyz_rgb(self, verbose=False):
     ''' Test some color values via check_xyz_rgb(). '''
     for i in range (100):
         x0 = 10.0 * random.random()
         y0 = 10.0 * random.random()
         z0 = 10.0 * random.random()
         xyz0 = colormodels.xyz_color (x0, y0, z0)
         self.check_xyz_rgb (xyz0, verbose)
示例#10
0
 def test_xyz_rgb(self, verbose=False):
     ''' Test some color values via check_xyz_rgb(). '''
     for i in range(100):
         x0 = 10.0 * random.random()
         y0 = 10.0 * random.random()
         z0 = 10.0 * random.random()
         xyz0 = colormodels.xyz_color(x0, y0, z0)
         self.check_xyz_rgb(xyz0, verbose)
 def test_xyz_irgb(self, verbose=False):
     ''' Test the direct conversions from xyz to irgb. '''
     for i in range (100):
         x0 = 10.0 * random.random()
         y0 = 10.0 * random.random()
         z0 = 10.0 * random.random()
         xyz0 = colormodels.xyz_color (x0,y0,z0)
         self.check_xyz_irgb(xyz0, verbose)
 def test_xyz_lab(self, verbose=False):
     '''Test that lab_from_xyz() and xyz_from_lab() are inverses.'''
     for i in range (100):
         x0 = 10.0 * random.random()
         y0 = 10.0 * random.random()
         z0 = 10.0 * random.random()
         xyz0 = colormodels.xyz_color (x0, y0, z0)
         self.check_xyz_lab(xyz0, verbose)
示例#13
0
 def test_xyz_irgb(self, verbose=False):
     ''' Test the direct conversions from xyz to irgb. '''
     for i in range(100):
         x0 = 10.0 * random.random()
         y0 = 10.0 * random.random()
         z0 = 10.0 * random.random()
         xyz0 = colormodels.xyz_color(x0, y0, z0)
         self.check_xyz_irgb(xyz0, verbose)
示例#14
0
def test_xyz_rgb(verbose=1):
    '''Test that xyz_to_rgb() and rgb_to_xyz() are inverses.'''
    def test_A(xyz0, tolerance=1.0e-10, verbose=1):
        rgb0 = colormodels.rgb_from_xyz(xyz0)
        xyz1 = colormodels.xyz_from_rgb(rgb0)
        rgb1 = colormodels.rgb_from_xyz(xyz1)
        # check errors
        err_rgb = rgb1 - rgb0
        error_rgb = math.sqrt(numpy.dot(err_rgb, err_rgb))
        err_xyz = xyz1 - xyz0
        error_xyz = math.sqrt(numpy.dot(err_xyz, err_xyz))
        passed = (error_rgb <= tolerance) and (error_xyz <= tolerance)
        if passed:
            status = 'pass'
        else:
            status = 'FAILED'
        msg = 'test_xyz_rgb.test_A() : xyz0 = %s, rgb(xyz0) = %s, xyz(rgb(xyz0)) = %s, rgb(xyz(rgb(xyz0))) = %s, errors = (%g, %g), %s' % (
            str(xyz0), str(rgb0), str(xyz1), str(rgb1), error_rgb, error_xyz,
            status)
        if verbose >= 1:
            print(msg)
        if not passed:
            pass
            raise ValueError(msg)
        return passed

    num_passed = 0
    num_failed = 0

    for i in xrange(0, 100):
        x0 = 10.0 * random.random()
        y0 = 10.0 * random.random()
        z0 = 10.0 * random.random()
        xyz0 = colormodels.xyz_color(x0, y0, z0)
        passed = test_A(xyz0, tolerance=1.0e-10, verbose=verbose)
        if passed:
            num_passed += 1
        else:
            num_failed += 1

    # Test that the conversion matrices are inverses
    test_eye0 = numpy.dot(colormodels.rgb_from_xyz_matrix,
                          colormodels.xyz_from_rgb_matrix)
    test_eye1 = numpy.dot(colormodels.xyz_from_rgb_matrix,
                          colormodels.rgb_from_xyz_matrix)
    passed = numpy.allclose(test_eye0, numpy.eye(3)) and numpy.allclose(
        test_eye1, numpy.eye(3))
    if passed:
        num_passed += 1
    else:
        num_failed += 1

    msg = 'test_xyz_rgb() : %d tests passed, %d tests failed' % (num_passed,
                                                                 num_failed)
    print(msg)
示例#15
0
def xyz_from_wavelength(wl_nm):
    '''Given a wavelength (nm), return the corresponding xyz color, for unit intensity.'''
    # separate wl_nm into integer and fraction
    int_wl_nm = math.floor(wl_nm)
    frac_wl_nm = wl_nm - float(int_wl_nm)
    # skip out of range (invisible) wavelengths
    if (int_wl_nm < start_wl_nm - 1) or (int_wl_nm > end_wl_nm + 1):
        return colormodels.xyz_color(0.0, 0.0, 0.0)
    # get index into main table
    index = int_wl_nm - start_wl_nm + 1
    # apply linear interpolation to get the color
    return _xyz_colors[index] + frac_wl_nm * _xyz_deltas[index]
示例#16
0
文件: ciexyz.py 项目: Stanpol/ColorPy
def xyz_from_wavelength (wl_nm):
    '''Given a wavelength (nm), return the corresponding xyz color, for unit intensity.'''
    # separate wl_nm into integer and fraction
    int_wl_nm = math.floor (wl_nm)
    frac_wl_nm = wl_nm - float (int_wl_nm)
    # skip out of range (invisible) wavelengths
    if (int_wl_nm < start_wl_nm - 1) or (int_wl_nm > end_wl_nm + 1):
        return colormodels.xyz_color (0.0, 0.0, 0.0)
    # get index into main table
    index = int_wl_nm - start_wl_nm + 1
    # apply linear interpolation to get the color
    return _xyz_colors [index] + frac_wl_nm * _xyz_deltas [index]
示例#17
0
def test_xyz_rgb (verbose=1):
    '''Test that xyz_to_rgb() and rgb_to_xyz() are inverses.'''

    def test_A (xyz0, tolerance=1.0e-10, verbose=1):
        rgb0 = colormodels.rgb_from_xyz (xyz0)
        xyz1 = colormodels.xyz_from_rgb (rgb0)
        rgb1 = colormodels.rgb_from_xyz (xyz1)
        # check errors
        err_rgb = rgb1 - rgb0
        error_rgb = math.sqrt (numpy.dot (err_rgb, err_rgb))
        err_xyz = xyz1 - xyz0
        error_xyz = math.sqrt (numpy.dot (err_xyz, err_xyz))
        passed = (error_rgb <= tolerance) and (error_xyz <= tolerance)
        if passed:
            status = 'pass'
        else:
            status = 'FAILED'
        msg = 'test_xyz_rgb.test_A() : xyz0 = %s, rgb(xyz0) = %s, xyz(rgb(xyz0)) = %s, rgb(xyz(rgb(xyz0))) = %s, errors = (%g, %g), %s' % (
            str (xyz0), str (rgb0), str (xyz1), str (rgb1), error_rgb, error_xyz, status)
        if verbose >= 1:
            print msg
        if not passed:
            pass
            raise ValueError, msg
        return passed

    num_passed = 0
    num_failed = 0

    for i in xrange (0, 100):
        x0 = 10.0 * random.random()
        y0 = 10.0 * random.random()
        z0 = 10.0 * random.random()
        xyz0 = colormodels.xyz_color (x0,y0,z0)
        passed = test_A (xyz0, tolerance=1.0e-10, verbose=verbose)
        if passed:
            num_passed += 1
        else:
            num_failed += 1

    # Test that the conversion matrices are inverses
    test_eye0 = numpy.dot (colormodels.rgb_from_xyz_matrix, colormodels.xyz_from_rgb_matrix)
    test_eye1 = numpy.dot (colormodels.xyz_from_rgb_matrix, colormodels.rgb_from_xyz_matrix)
    passed = numpy.allclose (test_eye0, numpy.eye (3)) and numpy.allclose (test_eye1, numpy.eye (3))
    if passed:
        num_passed += 1
    else:
        num_failed += 1

    msg = 'test_xyz_rgb() : %d tests passed, %d tests failed' % (
        num_passed, num_failed)
    print msg
示例#18
0
def chemical_solutions_patch_plot():
    '''Colors of some chemical solutions.
    Darren L. Williams et. al., 'Beyond lambda-max: Transforming Visible Spectra into 24-bit Color Values'.
        Journal of Chemical Education, Vol 84, No 11, Nov 2007, p1873-1877.
    A student laboratory experiment to measure the transmission spectra of some common chemical solutions,
    and determine the rgb values.'''
    xyz_colors = []
    xyz_colors.append(colormodels.xyz_color(0.360, 0.218, 0.105))
    xyz_colors.append(colormodels.xyz_color(0.458, 0.691, 0.587))
    xyz_colors.append(colormodels.xyz_color(0.445, 0.621, 1.052))
    xyz_colors.append(colormodels.xyz_color(0.742, 0.579, 0.905))
    xyz_colors.append(colormodels.xyz_color(0.949, 1.000, 1.087))
    color_names = []
    color_names.append('1 M CoCl2')
    color_names.append('1 M NiCl2')
    color_names.append('1 M CuSO4')
    color_names.append('0.005 M KMnO4')
    color_names.append('H2O')
    plots.xyz_patch_plot(
        xyz_colors, color_names,
        'Colors of some chemical solutions\nJ. Chem. Ed., Vol 84, No 11, Nov 2007, p 1873-1877.',
        'ChemSolutions')
示例#19
0
def chemical_solutions_patch_plot ():
    '''Colors of some chemical solutions.
    Darren L. Williams et. al., 'Beyond lambda-max: Transforming Visible Spectra into 24-bit Color Values'.
        Journal of Chemical Education, Vol 84, No 11, Nov 2007, p1873-1877.
    A student laboratory experiment to measure the transmission spectra of some common chemical solutions,
    and determine the rgb values.'''
    xyz_colors = []
    xyz_colors.append (colormodels.xyz_color (0.360, 0.218, 0.105))
    xyz_colors.append (colormodels.xyz_color (0.458, 0.691, 0.587))
    xyz_colors.append (colormodels.xyz_color (0.445, 0.621, 1.052))
    xyz_colors.append (colormodels.xyz_color (0.742, 0.579, 0.905))
    xyz_colors.append (colormodels.xyz_color (0.949, 1.000, 1.087))
    color_names = []
    color_names.append ('1 M CoCl2')
    color_names.append ('1 M NiCl2')
    color_names.append ('1 M CuSO4')
    color_names.append ('0.005 M KMnO4')
    color_names.append ('H2O')
    plots.xyz_patch_plot (
        xyz_colors,
        color_names,
        'Colors of some chemical solutions\nJ. Chem. Ed., Vol 84, No 11, Nov 2007, p 1873-1877.',
        'ChemSolutions')
示例#20
0
def test_xyz_irgb(verbose=1):
    '''Test the direct conversions from xyz to irgb.'''
    for i in xrange(0, 100):
        x0 = 10.0 * random.random()
        y0 = 10.0 * random.random()
        z0 = 10.0 * random.random()
        xyz0 = colormodels.xyz_color(x0, y0, z0)
        irgb0 = colormodels.irgb_from_rgb(colormodels.rgb_from_xyz(xyz0))
        irgb1 = colormodels.irgb_from_xyz(xyz0)
        if (irgb0[0] != irgb1[0]) or (irgb0[1] != irgb1[1]) or (irgb0[2] !=
                                                                irgb1[2]):
            raise ValueError
        irgbs0 = colormodels.irgb_string_from_rgb(
            colormodels.rgb_from_xyz(xyz0))
        irgbs1 = colormodels.irgb_string_from_xyz(xyz0)
        if irgbs0 != irgbs1:
            raise ValueError
    print('Passed test_xyz_irgb()')
示例#21
0
def test_xyz_irgb (verbose=1):
    '''Test the direct conversions from xyz to irgb.'''
    for i in xrange (0, 100):
        x0 = 10.0 * random.random()
        y0 = 10.0 * random.random()
        z0 = 10.0 * random.random()
        xyz0 = colormodels.xyz_color (x0,y0,z0)
        irgb0 = colormodels.irgb_from_rgb (
            colormodels.rgb_from_xyz (xyz0))
        irgb1 = colormodels.irgb_from_xyz (xyz0)
        if (irgb0[0] != irgb1[0]) or (irgb0[1] != irgb1[1]) or (irgb0[2] != irgb1[2]):
            raise ValueError
        irgbs0 = colormodels.irgb_string_from_rgb (
            colormodels.rgb_from_xyz (xyz0))
        irgbs1 = colormodels.irgb_string_from_xyz (xyz0)
        if irgbs0 != irgbs1:
            raise ValueError
    print 'Passed test_xyz_irgb()'
示例#22
0
def xyz_from_spectrum(spectrum):
    '''Determine the xyz color of the spectrum.

    The spectrum is assumed to be a 2D numpy array, with a row for each wavelength,
    and two columns.  The first column should hold the wavelength (nm), and the
    second should hold the light intensity.  The set of wavelengths can be arbitrary,
    it does not have to be the set that empty_spectrum() returns.'''
    shape = numpy.shape(spectrum)
    (num_wl, num_col) = shape
    assert num_col == 2, 'Expecting 2D array with each row: wavelength [nm], specific intensity [W/unit solid angle]'
    # integrate
    rtn = colormodels.xyz_color(0.0, 0.0, 0.0)
    for i in xrange(0, num_wl):
        wl_nm_i = spectrum[i][0]
        specific_intensity_i = spectrum[i][1]
        xyz = xyz_from_wavelength(wl_nm_i)
        rtn += specific_intensity_i * xyz
    return rtn
示例#23
0
文件: ciexyz.py 项目: Stanpol/ColorPy
def xyz_from_spectrum (spectrum):
    '''Determine the xyz color of the spectrum.

    The spectrum is assumed to be a 2D numpy array, with a row for each wavelength,
    and two columns.  The first column should hold the wavelength (nm), and the
    second should hold the light intensity.  The set of wavelengths can be arbitrary,
    it does not have to be the set that empty_spectrum() returns.'''
    shape = numpy.shape (spectrum)
    (num_wl, num_col) = shape
    assert num_col == 2, 'Expecting 2D array with each row: wavelength [nm], specific intensity [W/unit solid angle]'
    # integrate
    rtn = colormodels.xyz_color (0.0, 0.0, 0.0)
    for i in xrange (0, num_wl):
        wl_nm_i = spectrum [i][0]
        specific_intensity_i = spectrum [i][1]
        xyz = xyz_from_wavelength (wl_nm_i)
        rtn += specific_intensity_i * xyz
    return rtn
示例#24
0
def MacBeth_ColorChecker_patch_plot ():
    '''MacBeth ColorChecker Chart.
    The xyz values are from Hall p. 119.  I do not know for what lighting conditions this applies.'''
    xyz_colors = []
    xyz_colors.append (colormodels.xyz_color (0.092, 0.081, 0.058))
    xyz_colors.append (colormodels.xyz_color (0.411, 0.376, 0.303))
    xyz_colors.append (colormodels.xyz_color (0.183, 0.186, 0.373))
    xyz_colors.append (colormodels.xyz_color (0.094, 0.117, 0.067))
    xyz_colors.append (colormodels.xyz_color (0.269, 0.244, 0.503))
    xyz_colors.append (colormodels.xyz_color (0.350, 0.460, 0.531))
    xyz_colors.append (colormodels.xyz_color (0.386, 0.311, 0.066))
    xyz_colors.append (colormodels.xyz_color (0.123, 0.102, 0.359))
    xyz_colors.append (colormodels.xyz_color (0.284, 0.192, 0.151))
    xyz_colors.append (colormodels.xyz_color (0.059, 0.040, 0.102))
    xyz_colors.append (colormodels.xyz_color (0.368, 0.474, 0.127))
    xyz_colors.append (colormodels.xyz_color (0.497, 0.460, 0.094))
    xyz_colors.append (colormodels.xyz_color (0.050, 0.035, 0.183))
    xyz_colors.append (colormodels.xyz_color (0.149, 0.234, 0.106))
    xyz_colors.append (colormodels.xyz_color (0.176, 0.102, 0.048))
    xyz_colors.append (colormodels.xyz_color (0.614, 0.644, 0.112))
    xyz_colors.append (colormodels.xyz_color (0.300, 0.192, 0.332))
    xyz_colors.append (colormodels.xyz_color (0.149, 0.192, 0.421))
    xyz_colors.append (colormodels.xyz_color (0.981, 1.000, 1.184))
    xyz_colors.append (colormodels.xyz_color (0.632, 0.644, 0.763))
    xyz_colors.append (colormodels.xyz_color (0.374, 0.381, 0.451))
    xyz_colors.append (colormodels.xyz_color (0.189, 0.192, 0.227))
    xyz_colors.append (colormodels.xyz_color (0.067, 0.068, 0.080))
    xyz_colors.append (colormodels.xyz_color (0.000, 0.000, 0.000))

    color_names = []
    color_names.append ('dark skin')
    color_names.append ('light skin')
    color_names.append ('blue sky')
    color_names.append ('foliage')
    color_names.append ('blue flower')
    color_names.append ('bluish green')
    color_names.append ('orange')
    color_names.append ('purplish blue')
    color_names.append ('moderate red')
    color_names.append ('purple')
    color_names.append ('yellow green')
    color_names.append ('orange yellow')
    color_names.append ('blue')
    color_names.append ('green')
    color_names.append ('red')
    color_names.append ('yellow')
    color_names.append ('magenta')
    color_names.append ('cyan')
    color_names.append ('white')
    color_names.append ('neutral 8')
    color_names.append ('neutral 6.5')
    color_names.append ('neutral 5')
    color_names.append ('neutral 3.5')
    color_names.append ('black')

    plots.xyz_patch_plot (
        xyz_colors,
        color_names,
        'MacBeth ColorChecker Chart',
        'MacBeth')
示例#25
0
 def test_xyz_lab_black(self, verbose=False):
     ''' Test Lab conversions on black. '''
     xyz0 = colormodels.xyz_color(0.0, 0.0, 0.0)
     self.check_xyz_lab(xyz0, verbose)
示例#26
0
def MacBeth_ColorChecker_patch_plot():
    '''MacBeth ColorChecker Chart.
    The xyz values are from Hall p. 119.  I do not know for what lighting conditions this applies.'''
    xyz_colors = []
    xyz_colors.append(colormodels.xyz_color(0.092, 0.081, 0.058))
    xyz_colors.append(colormodels.xyz_color(0.411, 0.376, 0.303))
    xyz_colors.append(colormodels.xyz_color(0.183, 0.186, 0.373))
    xyz_colors.append(colormodels.xyz_color(0.094, 0.117, 0.067))
    xyz_colors.append(colormodels.xyz_color(0.269, 0.244, 0.503))
    xyz_colors.append(colormodels.xyz_color(0.350, 0.460, 0.531))
    xyz_colors.append(colormodels.xyz_color(0.386, 0.311, 0.066))
    xyz_colors.append(colormodels.xyz_color(0.123, 0.102, 0.359))
    xyz_colors.append(colormodels.xyz_color(0.284, 0.192, 0.151))
    xyz_colors.append(colormodels.xyz_color(0.059, 0.040, 0.102))
    xyz_colors.append(colormodels.xyz_color(0.368, 0.474, 0.127))
    xyz_colors.append(colormodels.xyz_color(0.497, 0.460, 0.094))
    xyz_colors.append(colormodels.xyz_color(0.050, 0.035, 0.183))
    xyz_colors.append(colormodels.xyz_color(0.149, 0.234, 0.106))
    xyz_colors.append(colormodels.xyz_color(0.176, 0.102, 0.048))
    xyz_colors.append(colormodels.xyz_color(0.614, 0.644, 0.112))
    xyz_colors.append(colormodels.xyz_color(0.300, 0.192, 0.332))
    xyz_colors.append(colormodels.xyz_color(0.149, 0.192, 0.421))
    xyz_colors.append(colormodels.xyz_color(0.981, 1.000, 1.184))
    xyz_colors.append(colormodels.xyz_color(0.632, 0.644, 0.763))
    xyz_colors.append(colormodels.xyz_color(0.374, 0.381, 0.451))
    xyz_colors.append(colormodels.xyz_color(0.189, 0.192, 0.227))
    xyz_colors.append(colormodels.xyz_color(0.067, 0.068, 0.080))
    xyz_colors.append(colormodels.xyz_color(0.000, 0.000, 0.000))

    color_names = []
    color_names.append('dark skin')
    color_names.append('light skin')
    color_names.append('blue sky')
    color_names.append('foliage')
    color_names.append('blue flower')
    color_names.append('bluish green')
    color_names.append('orange')
    color_names.append('purplish blue')
    color_names.append('moderate red')
    color_names.append('purple')
    color_names.append('yellow green')
    color_names.append('orange yellow')
    color_names.append('blue')
    color_names.append('green')
    color_names.append('red')
    color_names.append('yellow')
    color_names.append('magenta')
    color_names.append('cyan')
    color_names.append('white')
    color_names.append('neutral 8')
    color_names.append('neutral 6.5')
    color_names.append('neutral 5')
    color_names.append('neutral 3.5')
    color_names.append('black')

    plots.xyz_patch_plot(xyz_colors, color_names, 'MacBeth ColorChecker Chart',
                         'MacBeth')
 def test_xyz_lab_black(self, verbose=False):
     ''' Test Lab conversions on black. '''
     xyz0 = colormodels.xyz_color (0.0, 0.0, 0.0)
     self.check_xyz_lab(xyz0, verbose)
示例#28
0
def test_uv_primes(verbose=1):
    '''Test that uv_primes() and uv_primes_inverse() are inverses.'''
    def test_A(xyz0, tolerance=0.0, verbose=1):
        (up0, vp0) = colormodels.uv_primes(xyz0)
        xyz1 = colormodels.uv_primes_inverse(up0, vp0, xyz0[1])
        # check error
        dxyz = (xyz1 - xyz0)
        error = math.sqrt(numpy.dot(dxyz, dxyz))
        passed = (error <= tolerance)
        if passed:
            status = 'pass'
        else:
            status = 'FAILED'
        msg = 'test_uv_primes.test_A() : xyz0 = %s, (up,vp) = (%g,%g), xyz(up,vp) = %s, error = %g, %s' % (
            str(xyz0), up0, vp0, str(xyz1), error, status)
        if verbose >= 1:
            print(msg)
        if not passed:
            pass
            raise ValueError(msg)
        return passed

    def test_B(up0, vp0, y0, tolerance=0.0, verbose=1):
        xyz0 = colormodels.uv_primes_inverse(up0, vp0, y0)
        (up1, vp1) = colormodels.uv_primes(xyz0)
        # check error
        error_up = up1 - up0
        error_vp = vp1 - vp0
        error = numpy.hypot(error_up, error_vp)
        passed = (error <= tolerance)
        if passed:
            status = 'pass'
        else:
            status = 'FAILED'
        msg = 'test_uv_primes.test_B() : (up0,vp0,y0) = (%g,%g,%g), xyz (up0,vp0,y0) = %s, (up,vp)(xyz) = (%g,%g), error = %g, %s' % (
            up0, vp0, y0, str(xyz0), up1, vp1, error, status)
        if verbose >= 1:
            print(msg)
        if not passed:
            pass
            raise ValueError(msg)
        return passed

    num_passed = 0
    num_failed = 0

    # Test A
    for i in xrange(0, 100):
        x0 = 10.0 * random.random()
        y0 = 10.0 * random.random()
        z0 = 10.0 * random.random()
        xyz0 = colormodels.xyz_color(x0, y0, z0)
        passed = test_A(xyz0, tolerance=1.0e-13, verbose=verbose)
        if passed:
            num_passed += 1
        else:
            num_failed += 1

    # Test black case explicitly
    xyz0 = colormodels.xyz_color(0.0, 0.0, 0.0)
    passed = test_A(xyz0, tolerance=0.0, verbose=verbose)
    if passed:
        num_passed += 1
    else:
        num_failed += 1

    # Test B
    for i in xrange(0, 100):
        up0 = 4.0 * (2.0 * random.random() - 1.0)
        vp0 = 9.0 * (2.0 * random.random() - 1.0)
        y0 = 10.0 * random.random()
        passed = test_B(up0, vp0, y0, tolerance=1.0e-13, verbose=verbose)
        if passed:
            num_passed += 1
        else:
            num_failed += 1

    # Test black case explicitly
    passed = test_B(0.0, 0.0, 0.0, tolerance=0.0, verbose=verbose)
    if passed:
        num_passed += 1
    else:
        num_failed += 1

    msg = 'test_uv_primes() : %d tests passed, %d tests failed' % (num_passed,
                                                                   num_failed)
    print(msg)
示例#29
0
def test_uv_primes (verbose=1):
    '''Test that uv_primes() and uv_primes_inverse() are inverses.'''

    def test_A (xyz0, tolerance=0.0, verbose=1):
        (up0, vp0) = colormodels.uv_primes (xyz0)
        xyz1 = colormodels.uv_primes_inverse (up0, vp0, xyz0[1])
        # check error
        dxyz = (xyz1 - xyz0)
        error = math.sqrt (numpy.dot (dxyz, dxyz))
        passed = (error <= tolerance)
        if passed:
            status = 'pass'
        else:
            status = 'FAILED'
        msg = 'test_uv_primes.test_A() : xyz0 = %s, (up,vp) = (%g,%g), xyz(up,vp) = %s, error = %g, %s' % (
            str (xyz0), up0, vp0, str(xyz1), error, status)
        if verbose >= 1:
            print msg
        if not passed:
            pass
            raise ValueError, msg
        return passed

    def test_B (up0, vp0, y0, tolerance=0.0, verbose=1):
        xyz0 = colormodels.uv_primes_inverse (up0, vp0, y0)
        (up1, vp1) = colormodels.uv_primes (xyz0)
        # check error
        error_up = up1 - up0
        error_vp = vp1 - vp0
        error = numpy.hypot (error_up, error_vp)
        passed = (error <= tolerance)
        if passed:
            status = 'pass'
        else:
            status = 'FAILED'
        msg = 'test_uv_primes.test_B() : (up0,vp0,y0) = (%g,%g,%g), xyz (up0,vp0,y0) = %s, (up,vp)(xyz) = (%g,%g), error = %g, %s' % (
            up0, vp0, y0, str (xyz0), up1, vp1, error, status)
        if verbose >= 1:
            print msg
        if not passed:
            pass
            raise ValueError, msg
        return passed
        
    num_passed = 0
    num_failed = 0

    # Test A
    for i in xrange (0, 100):
        x0 = 10.0 * random.random()
        y0 = 10.0 * random.random()
        z0 = 10.0 * random.random()
        xyz0 = colormodels.xyz_color (x0,y0,z0)
        passed = test_A (xyz0, tolerance=1.0e-13, verbose=verbose)
        if passed:
            num_passed += 1
        else:
            num_failed += 1
            
    # Test black case explicitly
    xyz0 = colormodels.xyz_color (0.0, 0.0, 0.0)    
    passed = test_A (xyz0, tolerance=0.0, verbose=verbose)
    if passed:
        num_passed += 1
    else:
        num_failed += 1

    # Test B
    for i in xrange (0, 100):
        up0 = 4.0 * (2.0 * random.random() - 1.0)
        vp0 = 9.0 * (2.0 * random.random() - 1.0)
        y0 = 10.0 * random.random()
        passed = test_B (up0, vp0, y0, tolerance=1.0e-13, verbose=verbose)
        if passed:
            num_passed += 1
        else:
            num_failed += 1

    # Test black case explicitly
    passed = test_B (0.0, 0.0, 0.0, tolerance=0.0, verbose=verbose)
    if passed:
        num_passed += 1
    else:
        num_failed += 1

    msg = 'test_uv_primes() : %d tests passed, %d tests failed' % (
        num_passed, num_failed)
    print msg