def check_rgb_irgb(self, irgb0, verbose):
     ''' Check that conversions between rgb and irgb are invertible. '''
     rgb0  = colormodels.rgb_from_irgb (irgb0)
     irgb1 = colormodels.irgb_from_rgb (rgb0)
     rgb1  = colormodels.rgb_from_irgb (irgb1)
     # Integer conversion should match exactly.
     self.assertEqual(irgb0[0], irgb1[0])
     self.assertEqual(irgb0[1], irgb1[1])
     self.assertEqual(irgb0[2], irgb1[2])
     msg = 'irgb0: %s    irgb1: %s' % (str(irgb0), str(irgb1))
     if verbose:
         print (msg)
     # Float conversion should match closely.
     # (It actually seems to match exactly for me.)
     tolerance = 1.0e-14
     err_rgb = rgb1 - rgb0
     err_r = math.fabs (err_rgb [0])
     err_g = math.fabs (err_rgb [1])
     err_b = math.fabs (err_rgb [2])
     self.assertLessEqual(err_r, tolerance)
     self.assertLessEqual(err_g, tolerance)
     self.assertLessEqual(err_b, tolerance)
     msg = 'rgb0: %s    rgb1: %s' % (str(rgb0), str(rgb1))
     if verbose:
         print (msg)
Esempio n. 2
0
 def check_rgb_irgb(self, irgb0, verbose):
     ''' Check that conversions between rgb and irgb are invertible. '''
     rgb0 = colormodels.rgb_from_irgb(irgb0)
     irgb1 = colormodels.irgb_from_rgb(rgb0)
     rgb1 = colormodels.rgb_from_irgb(irgb1)
     # Integer conversion should match exactly.
     self.assertEqual(irgb0[0], irgb1[0])
     self.assertEqual(irgb0[1], irgb1[1])
     self.assertEqual(irgb0[2], irgb1[2])
     msg = 'irgb0: %s    irgb1: %s' % (str(irgb0), str(irgb1))
     if verbose:
         print(msg)
     # Float conversion should match closely.
     # (It actually seems to match exactly for me.)
     tolerance = 1.0e-14
     err_rgb = rgb1 - rgb0
     err_r = math.fabs(err_rgb[0])
     err_g = math.fabs(err_rgb[1])
     err_b = math.fabs(err_rgb[2])
     self.assertLessEqual(err_r, tolerance)
     self.assertLessEqual(err_g, tolerance)
     self.assertLessEqual(err_b, tolerance)
     msg = 'rgb0: %s    rgb1: %s' % (str(rgb0), str(rgb1))
     if verbose:
         print(msg)
Esempio n. 3
0
def test_rgb_irgb(verbose=1):
    '''Test that conversions between rgb and irgb are invertible.'''
    for i in xrange(0, 100):
        ir = random.randrange(0, 256)
        ig = random.randrange(0, 256)
        ib = random.randrange(0, 256)
        irgb0 = colormodels.irgb_color(ir, ig, ib)
        rgb0 = colormodels.rgb_from_irgb(irgb0)
        irgb1 = colormodels.irgb_from_rgb(rgb0)
        rgb1 = colormodels.rgb_from_irgb(irgb1)
        if (irgb0[0] != irgb1[0]) or (irgb0[1] != irgb1[1]) or (irgb0[2] !=
                                                                irgb1[2]):
            msg = 'irgb0 %s and irgb1 %s do not match' % (str(irgb0),
                                                          str(irgb1))
            raise ValueError(msg)
        tolerance = 1.0e-14
        err_rgb = rgb1 - rgb0
        err_r = math.fabs(err_rgb[0])
        err_g = math.fabs(err_rgb[1])
        err_b = math.fabs(err_rgb[2])
        if (err_r > tolerance) or (err_g > tolerance) or (err_b > tolerance):
            msg = 'rgb0 %s and rgb1 %s differ by %g' % (
                str(rgb0), str(rgb1), max(err_r, err_g, err_b))
            raise ValueError(msg)
    if verbose >= 1:
        print('Passed test_rgb_irgb()')
Esempio n. 4
0
 def check_xyz_irgb(self, xyz0, verbose):
     ''' Check the direct conversions from xyz to irgb. '''
     irgb0 = colormodels.irgb_from_rgb(colormodels.rgb_from_xyz(xyz0))
     irgb1 = colormodels.irgb_from_xyz(xyz0)
     self.assertEqual(irgb0[0], irgb1[0])
     self.assertEqual(irgb0[1], irgb1[1])
     self.assertEqual(irgb0[2], irgb1[2])
     # The string should also match.
     irgbs0 = colormodels.irgb_string_from_rgb(
         colormodels.rgb_from_xyz(xyz0))
     irgbs1 = colormodels.irgb_string_from_xyz(xyz0)
     msg = 'irgb0: %s    text0: %s        irgb1: %s    text1: %s' % (
         str(irgb0), irgbs0, str(irgb1), irgbs1)
     if verbose:
         print(msg)
     self.assertEqual(irgbs0, irgbs1)
 def check_xyz_irgb(self, xyz0, verbose):
     ''' Check the direct conversions from xyz to irgb. '''
     irgb0 = colormodels.irgb_from_rgb (
         colormodels.rgb_from_xyz (xyz0))
     irgb1 = colormodels.irgb_from_xyz (xyz0)
     self.assertEqual(irgb0[0], irgb1[0])
     self.assertEqual(irgb0[1], irgb1[1])
     self.assertEqual(irgb0[2], irgb1[2])
     # The string should also match.
     irgbs0 = colormodels.irgb_string_from_rgb (
         colormodels.rgb_from_xyz (xyz0))
     irgbs1 = colormodels.irgb_string_from_xyz (xyz0)
     msg = 'irgb0: %s    text0: %s        irgb1: %s    text1: %s' % (
         str(irgb0), irgbs0, str(irgb1), irgbs1)
     if verbose:
         print (msg)
     self.assertEqual(irgbs0, irgbs1)
Esempio n. 6
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()')
Esempio n. 7
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()'
Esempio n. 8
0
def test_rgb_irgb (verbose=1):
    '''Test that conversions between rgb and irgb are invertible.'''
    for i in xrange (0, 100):
        ir = random.randrange (0, 256)
        ig = random.randrange (0, 256)
        ib = random.randrange (0, 256)
        irgb0 = colormodels.irgb_color (ir, ig, ib)
        rgb0 = colormodels.rgb_from_irgb (irgb0)
        irgb1 = colormodels.irgb_from_rgb (rgb0)
        rgb1 = colormodels.rgb_from_irgb (irgb1)
        if (irgb0[0] != irgb1[0]) or (irgb0[1] != irgb1[1]) or (irgb0[2] != irgb1[2]):
            msg = 'irgb0 %s and irgb1 %s do not match' % (str (irgb0), str (irgb1))
            raise ValueError, msg
        tolerance = 1.0e-14
        err_rgb = rgb1 - rgb0
        err_r = math.fabs (err_rgb [0])
        err_g = math.fabs (err_rgb [1])
        err_b = math.fabs (err_rgb [2])
        if (err_r > tolerance) or (err_g > tolerance) or (err_b > tolerance):
            msg = 'rgb0 %s and rgb1 %s differ by %g' % (str (rgb0), str (rgb1), max (err_r,err_g,err_b))
            raise ValueError, msg
    if verbose >= 1:
        print 'Passed test_rgb_irgb()'
Esempio n. 9
0
def visible_spectrum_table(filename='visible_spectrum.html'):
    '''Write an HTML table with the visible spectrum colors.'''
    spectrum = ciexyz.empty_spectrum()
    (num_wl, num_cols) = spectrum.shape
    # get rgb colors for each wavelength
    rgb_colors_1 = numpy.empty((num_wl, 3))
    rgb_colors_2 = numpy.empty((num_wl, 3))
    for i in range(0, num_wl):
        xyz = ciexyz.xyz_from_wavelength(spectrum[i][0])
        rgb_1 = colormodels.rgb_from_xyz(xyz)
        rgb_2 = colormodels.brightest_rgb_from_xyz(xyz)
        rgb_colors_1[i] = rgb_1
        rgb_colors_2[i] = rgb_2
    # scale 1 to make brightest rgb value = 1.0
    rgb_max = numpy.max(rgb_colors_1)
    scaling = 1.0 / rgb_max
    rgb_colors_1 *= scaling

    # write HTML file

    def write_link(f, url, text):
        '''Write an html link.'''
        link = '<a href="%s">%s</a><br/>\n' % (url, text)
        f.write(link)

    f = open(filename, 'w')
    # html headers
    f.write('<html>\n')
    f.write('<head>\n')
    f.write('<title>Colors of Pure Spectral Lines</title>\n')
    f.write('</head>\n')
    f.write('<body>\n')
    f.write('<p><h1>Colors of Pure Spectral Lines</h1></p>\n')
    f.write('<p>%s</p>\n' %
            'White added to undisplayable pure colors to fit into rgb space.')
    f.write('<hr/>\n')
    # table header
    f.write('<table border cellpadding="5">\n')
    f.write('<tr>\n')
    f.write('<th>Wavelength</th>\n')
    f.write('<th>R</th>\n')
    f.write('<th>G</th>\n')
    f.write('<th>B</th>\n')
    f.write('<th>Hex Code</th>\n')
    f.write('<th width=200>Full Brightness</th>\n')
    f.write('<th width=200>Perceptual Brightness</th>\n')
    f.write('</tr>\n')
    # each row

    for i in range(0, num_wl):
        irgb_1 = colormodels.irgb_from_rgb(rgb_colors_1[i])
        irgb_2 = colormodels.irgb_from_rgb(rgb_colors_2[i])
        red = irgb_2[0]
        green = irgb_2[1]
        blue = irgb_2[2]
        hexstr_1 = colormodels.irgb_string_from_irgb(irgb_1)
        hexstr_2 = colormodels.irgb_string_from_irgb(irgb_2)

        iwl = spectrum[i][0]
        code = '%.1f nm' % iwl

        f.write('<tr>\n')
        f.write('<td>%s</td>\n' % (code))
        f.write('<td>%d</td>\n' % (red))
        f.write('<td>%d</td>\n' % (green))
        f.write('<td>%d</td>\n' % (blue))
        f.write('<td>%s</td>\n' % (hexstr_2))
        swatch = "&nbsp;"
        f.write('<td bgcolor="%s">%s</td>\n' % (hexstr_2, swatch))
        f.write('<td bgcolor="%s">%s</td>\n' % (hexstr_1, swatch))
        f.write('</tr>\n')

    f.write('</table>\n')
    # references
    f.write('<hr/>\n')
    f.write('<p>References</p>\n')
    # one source for data
    write_link(f, 'http://goffgrafix.com/pantone-rgb-100.php',
               'Goffgrafix.com')
    # another source with basically the same data
    write_link(f, 'http://www.sandaleo.com/pantone.asp', 'Sandaleo.com')
    # one with more colors including metallic (also some errors), not quite consistent with the first two
    write_link(
        f, 'http://www.loral.org/Z/Colors/100.html',
        'Loral.org - Conversions based on CorelDRAW v12 Pantone Solid Coated or Pastel Coated tables and sRGB color space.'
    )
    # some colors for various sports teams
    write_link(
        f,
        'http://www.pennjersey.info/forums/questions-answers/7895-pantone-colors-colleges-university-mlb-nfl-teams.html',
        'Pantone colors for some sports teams.')
    # some colors for various national flags
    write_link(
        f, 'http://desktoppub.about.com/od/colorpalettes/l/aa_flagcolors.htm',
        'What color is your flag? Pantone colors for some flags.')
    write_link(f,
               'http://desktoppub.about.com/library/weekly/blcpflagsrwb.htm',
               'Red, White, &amp Blue - Pantone colors for some flags.')
    write_link(
        f, 'http://desktoppub.about.com/library/weekly/blcpflagsyellow.htm',
        'Yellow or Gold - Pantone colors for some flags.')
    write_link(
        f, 'http://desktoppub.about.com/library/weekly/blcpflagsgreen.htm',
        'Green - Pantone colors for some flags.')
    write_link(
        f,
        'http://desktoppub.about.com/library/weekly/blcpatrioticswatches.htm',
        'Color swatches - Pantone colors for some flags.')
    # official Pantone webpages
    write_link(f,
               'http://pantone.com/pages/pantone/Pantone.aspx?pg=19970&ca=25',
               'An official PANTONE page')
    write_link(f,
               'http://pantone.com/pages/products/product.aspx?ca=1&pid=293&',
               'Another official PANTONE page')
    # html ending
    f.write('</body>\n')
    f.write('</html>\n')
    f.close()
Esempio n. 10
0
def visible_spectrum_table (filename='visible_spectrum.html'):
    '''Write an HTML table with the visible spectrum colors.'''
    spectrum = ciexyz.empty_spectrum()
    (num_wl, num_cols) = spectrum.shape
    # get rgb colors for each wavelength
    rgb_colors_1 = numpy.empty ((num_wl, 3))
    rgb_colors_2 = numpy.empty ((num_wl, 3))
    for i in range (0, num_wl):
        xyz = ciexyz.xyz_from_wavelength (spectrum [i][0])
        rgb_1 = colormodels.rgb_from_xyz (xyz)
        rgb_2 = colormodels.brightest_rgb_from_xyz (xyz)
        rgb_colors_1 [i] = rgb_1
        rgb_colors_2 [i] = rgb_2
    # scale 1 to make brightest rgb value = 1.0
    rgb_max = numpy.max (rgb_colors_1)
    scaling = 1.0 / rgb_max
    rgb_colors_1 *= scaling
    # write HTML file

    def write_link (f, url, text):
        '''Write an html link.'''
        link = '<a href="%s">%s</a><br/>\n' % (url, text)
        f.write (link)

    f = open (filename, 'w')
    # html headers
    f.write ('<html>\n')
    f.write ('<head>\n')
    f.write ('<title>Colors of Pure Spectral Lines</title>\n')
    f.write ('</head>\n')
    f.write ('<body>\n')
    f.write ('<p><h1>Colors of Pure Spectral Lines</h1></p>\n')
    f.write ('<p>%s</p>\n' % 'White added to undisplayable pure colors to fit into rgb space.')
    f.write ('<hr/>\n')
    # table header
    f.write ('<table border cellpadding="5">\n')
    f.write ('<tr>\n')
    f.write ('<th>Wavelength</th>\n')
    f.write ('<th>R</th>\n')
    f.write ('<th>G</th>\n')
    f.write ('<th>B</th>\n')
    f.write ('<th>Hex Code</th>\n')
    f.write ('<th width=200>Full Brightness</th>\n')
    f.write ('<th width=200>Perceptual Brightness</th>\n')
    f.write ('</tr>\n')
    # each row

    for i in range (0, num_wl):
        irgb_1 = colormodels.irgb_from_rgb (rgb_colors_1 [i])
        irgb_2 = colormodels.irgb_from_rgb (rgb_colors_2 [i])
        red   = irgb_2 [0]
        green = irgb_2 [1]
        blue  = irgb_2 [2]
        hexstr_1 = colormodels.irgb_string_from_irgb (irgb_1)
        hexstr_2 = colormodels.irgb_string_from_irgb (irgb_2)

        iwl = spectrum [i][0]
        code = '%.1f nm' % iwl

        f.write ('<tr>\n')
        f.write ('<td>%s</td>\n' % (code))
        f.write ('<td>%d</td>\n' % (red))
        f.write ('<td>%d</td>\n' % (green))
        f.write ('<td>%d</td>\n' % (blue))
        f.write ('<td>%s</td>\n' % (hexstr_2))
        swatch = "&nbsp;"
        f.write ('<td bgcolor="%s">%s</td>\n' % (hexstr_2, swatch))
        f.write ('<td bgcolor="%s">%s</td>\n' % (hexstr_1, swatch))
        f.write ('</tr>\n')

    f.write ('</table>\n')
    # references
    f.write ('<hr/>\n')
    f.write ('<p>References</p>\n')
    # one source for data
    write_link (f, 'http://goffgrafix.com/pantone-rgb-100.php', 'Goffgrafix.com')
    # another source with basically the same data
    write_link (f, 'http://www.sandaleo.com/pantone.asp', 'Sandaleo.com')
    # one with more colors including metallic (also some errors), not quite consistent with the first two
    write_link (f, 'http://www.loral.org/Z/Colors/100.html',
        'Loral.org - Conversions based on CorelDRAW v12 Pantone Solid Coated or Pastel Coated tables and sRGB color space.')
    # some colors for various sports teams
    write_link (f, 'http://www.pennjersey.info/forums/questions-answers/7895-pantone-colors-colleges-university-mlb-nfl-teams.html',
        'Pantone colors for some sports teams.')
    # some colors for various national flags
    write_link (f, 'http://desktoppub.about.com/od/colorpalettes/l/aa_flagcolors.htm', 'What color is your flag? Pantone colors for some flags.')
    write_link (f, 'http://desktoppub.about.com/library/weekly/blcpflagsrwb.htm', 'Red, White, &amp Blue - Pantone colors for some flags.')
    write_link (f, 'http://desktoppub.about.com/library/weekly/blcpflagsyellow.htm', 'Yellow or Gold - Pantone colors for some flags.')
    write_link (f, 'http://desktoppub.about.com/library/weekly/blcpflagsgreen.htm', 'Green - Pantone colors for some flags.')
    write_link (f, 'http://desktoppub.about.com/library/weekly/blcpatrioticswatches.htm', 'Color swatches - Pantone colors for some flags.')
    # official Pantone webpages
    write_link (f, 'http://pantone.com/pages/pantone/Pantone.aspx?pg=19970&ca=25', 'An official PANTONE page')
    write_link (f, 'http://pantone.com/pages/products/product.aspx?ca=1&pid=293&', 'Another official PANTONE page')
    # html ending
    f.write ('</body>\n')
    f.write ('</html>\n')
    f.close()