def test_snapshot(self):
        for hex_color, colors in self.snapshot.items():

            # Test forward functions
            test_rgb = husl.hex_to_rgb(hex_color)
            self.assertTuplesClose(test_rgb, colors['rgb'])
            test_xyz = husl.rgb_to_xyz(test_rgb)
            self.assertTuplesClose(test_xyz, colors['xyz'])
            test_luv = husl.xyz_to_luv(test_xyz)
            self.assertTuplesClose(test_luv, colors['luv'])
            test_lch = husl.luv_to_lch(test_luv)
            self.assertTuplesClose(test_lch, colors['lch'])
            test_husl = husl.lch_to_husl(test_lch)
            self.assertTuplesClose(test_husl, colors['husl'])
            test_huslp = husl.lch_to_huslp(test_lch)
            self.assertTuplesClose(test_huslp, colors['huslp'])

            # Test backward functions
            test_lch = husl.husl_to_lch(colors['husl'])
            self.assertTuplesClose(test_lch, colors['lch'])
            test_lch = husl.huslp_to_lch(colors['huslp'])
            self.assertTuplesClose(test_lch, colors['lch'])
            test_luv = husl.lch_to_luv(test_lch)
            self.assertTuplesClose(test_luv, colors['luv'])
            test_xyz = husl.luv_to_xyz(test_luv)
            self.assertTuplesClose(test_xyz, colors['xyz'])
            test_rgb = husl.xyz_to_rgb(test_xyz)
            self.assertTuplesClose(test_rgb, colors['rgb'])
            self.assertEqual(husl.rgb_to_hex(test_rgb), hex_color)

            # Full test
            self.assertEqual(husl.husl_to_hex(*colors['husl']), hex_color)
            self.assertTuplesClose(husl.hex_to_husl(hex_color), colors['husl'])
            self.assertEqual(husl.huslp_to_hex(*colors['huslp']), hex_color)
            self.assertTuplesClose(husl.hex_to_huslp(hex_color), colors['huslp'])
Exemple #2
0
def parseColor(color):
    parsed_color = None
    if color.startswith('#'):
        if re.match('^#[0-9a-f]{6}$', color) != None:
            parsed_color = color
    elif color.startswith('rgb'):
        rgb_match = re.search(
            '^rgb\(([0-9]{1,3}),\s*([0-9]{1,3}),\s*([0-9]{1,3})\)$', color)
        if rgb_match is not None:
            try:
                r = min(int(rgb_match.group(1)), 255)
                g = min(int(rgb_match.group(2)), 255)
                b = min(int(rgb_match.group(3)), 255)
                parsed_color = '#' + ("%x" % r) + ("%x" % g) + ("%x" % b)
            except ValueError:
                pass
    elif color.startswith('hsl'):
        hsl_match = re.search(
            '^hsl\(([0-9]{1,3}(\.[0-9]*)?),\s*([0-9]{1,3}(\.[0-9]*)?)\%,\s*([0-9]{1,3}(\.[0-9]*)?)\%\)$',
            color)
        if hsl_match is not None:
            try:
                h = min(float(hsl_match.group(1)) / 365, 1)
                s = min(float(hsl_match.group(3)) / 100, 1)
                l = min(float(hsl_match.group(5)) / 100, 1)
                (r, g, b) = colorsys.hls_to_rgb(h, l, s)
                parsed_color = '#' + ("%x" %
                                      (r * 255)) + ("%x" %
                                                    (g * 255)) + ("%x" %
                                                                  (b * 255))
            except ValueError:
                pass
    elif color.startswith('husl'):
        husl_match = re.search(
            '^husl\(([0-9]{1,3}(\.[0-9]*)?),\s*([0-9]{1,3}(\.[0-9]*)?)\%,\s*([0-9]{1,3}(\.[0-9]*)?)\%\)$',
            color)
        if husl_match is not None:
            try:
                import husl
                h = min(float(husl_match.group(1)), 360)
                s = min(float(husl_match.group(3)), 100)
                l = min(float(husl_match.group(5)), 100)
                rgb = husl.husl_to_rgb(h, s, l)
                parsed_color = husl.rgb_to_hex(rgb)
            except ValueError:
                pass
            except ImportError:
                print(
                    'HUSL colour definitions need the husl library. Please install it.'
                )
                pass

    return parsed_color
def parseColor(color):
    parsed_color = None
    if color.startswith('#'):
        if re.match('^#[0-9a-f]{6}$', color) != None:
            parsed_color = color
    elif color.startswith('rgb'):
        rgb_match = re.search('^rgb\(([0-9]{1,3}),\s*([0-9]{1,3}),\s*([0-9]{1,3})\)$', color)
        if rgb_match is not None:
            try:
                r = min(int(rgb_match.group(1)), 255)
                g = min(int(rgb_match.group(2)), 255)
                b = min(int(rgb_match.group(3)), 255)
                parsed_color = '#'+("%x" % r)+("%x" % g)+("%x" % b)
            except ValueError:
                pass
    elif color.startswith('hsl'):
        hsl_match = re.search('^hsl\(([0-9]{1,3}(\.[0-9]*)?),\s*([0-9]{1,3}(\.[0-9]*)?)\%,\s*([0-9]{1,3}(\.[0-9]*)?)\%\)$', color)
        if hsl_match is not None:
            try:
                h = min(float(hsl_match.group(1)) / 365, 1)
                s = min(float(hsl_match.group(3)) / 100, 1)
                l = min(float(hsl_match.group(5)) / 100, 1)
                (r, g, b) = colorsys.hls_to_rgb(h, l, s)
                parsed_color = '#'+("%x" % (r * 255))+("%x" % (g * 255))+("%x" % (b * 255))
            except ValueError:
                pass
    elif color.startswith('husl'):
        husl_match = re.search('^husl\(([0-9]{1,3}(\.[0-9]*)?),\s*([0-9]{1,3}(\.[0-9]*)?)\%,\s*([0-9]{1,3}(\.[0-9]*)?)\%\)$', color)
        if husl_match is not None:
            try:
                import husl
                h = min(float(husl_match.group(1)), 360)
                s = min(float(husl_match.group(3)), 100)
                l = min(float(husl_match.group(5)), 100)
                rgb = husl.husl_to_rgb(h, s, l)
                parsed_color = husl.rgb_to_hex(rgb)
            except ValueError:
                pass
            except ImportError:
                print('HUSL colour definitions need the husl library. Please install it.')
                pass

    return parsed_color
Exemple #4
0
    def test_snapshot(self):
        for hex_color, colors in self.snapshot.items():

            # Test forward functions
            test_rgb = husl.hex_to_rgb(hex_color)
            self.assertTuplesClose(test_rgb, colors['rgb'])
            test_xyz = husl.rgb_to_xyz(test_rgb)
            self.assertTuplesClose(test_xyz, colors['xyz'])
            test_luv = husl.xyz_to_luv(test_xyz)
            self.assertTuplesClose(test_luv, colors['luv'])
            test_lch = husl.luv_to_lch(test_luv)
            self.assertTuplesClose(test_lch, colors['lch'])
            test_husl = husl.lch_to_husl(test_lch)
            self.assertTuplesClose(test_husl, colors['husl'])
            test_huslp = husl.lch_to_huslp(test_lch)
            self.assertTuplesClose(test_huslp, colors['huslp'])

            # Test backward functions
            test_lch = husl.husl_to_lch(colors['husl'])
            self.assertTuplesClose(test_lch, colors['lch'])
            test_lch = husl.huslp_to_lch(colors['huslp'])
            self.assertTuplesClose(test_lch, colors['lch'])
            test_luv = husl.lch_to_luv(test_lch)
            self.assertTuplesClose(test_luv, colors['luv'])
            test_xyz = husl.luv_to_xyz(test_luv)
            self.assertTuplesClose(test_xyz, colors['xyz'])
            test_rgb = husl.xyz_to_rgb(test_xyz)
            self.assertTuplesClose(test_rgb, colors['rgb'])
            self.assertEqual(husl.rgb_to_hex(test_rgb), hex_color)

            # Full test
            self.assertEqual(husl.husl_to_hex(*colors['husl']), hex_color)
            self.assertTuplesClose(husl.hex_to_husl(hex_color), colors['husl'])
            self.assertEqual(husl.huslp_to_hex(*colors['huslp']), hex_color)
            self.assertTuplesClose(husl.hex_to_huslp(hex_color),
                                   colors['huslp'])