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)
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()')
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)
def colorstring_patch_plot (colorstrings, color_names, title, filename, num_across=6): '''Color patch plot for colors specified as hex strings.''' rgb_colors = [] for color in colorstrings: irgb = colormodels.irgb_from_irgb_string (color) rgb = colormodels.rgb_from_irgb (irgb) rgb_colors.append (rgb) plots.rgb_patch_plot ( rgb_colors, color_names, title, filename, num_across=num_across)
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()'
def colorstring_patch_plot(colorstrings, color_names, title, filename, num_across=6): '''Color patch plot for colors specified as hex strings.''' rgb_colors = [] for color in colorstrings: irgb = colormodels.irgb_from_irgb_string(color) rgb = colormodels.rgb_from_irgb(irgb) rgb_colors.append(rgb) plots.rgb_patch_plot(rgb_colors, color_names, title, filename, num_across=num_across)