Example #1
0
 def test_static_conversion_utilities(self):
     # Tests initializing a color by name or hex string value
     # (names and hex values are not case-sensitive)
     self.assertEqual((240, 248, 255), RGBColor.string_to_rgb('aliceblue'))
     self.assertEqual((240, 248, 255), RGBColor.name_to_rgb('aliceblue'))
     self.assertEqual((240, 248, 255), RGBColor.hex_to_rgb('F0F8FF'))
     self.assertEqual((240, 248, 255), RGBColor.string_to_rgb('F0F8FF'))
     with self.assertRaises(AssertionError):
         RGBColor.string_to_rgb('non_existant')
     self.assertEqual((0, 0, 0), RGBColor.name_to_rgb('non_existant'))
     self.assertEqual((240, 248, 255), RGBColor.hex_to_rgb('f0f8ff'))
     self.assertEqual((240, 248, 255), RGBColor.string_to_rgb('f0f8ff'))
Example #2
0
    def _get_color(self, color):
        color_tuple = RGBColor.hex_to_rgb(color)

        for color_name, val in named_rgb_colors.items():
            if color_tuple == val:
                self.log.debug("Converting hex color '%s' to named color "
                               "'%s'", color, color_name)
                return color_name

        return color
Example #3
0
    def _validate_type_color(self, item, validation_failure_info):
        if isinstance(item, tuple):
            if len(item) != 3:
                self.validation_error(item, validation_failure_info, "Color needs three components")
            return item

        # Validates colors by name, hex, or list, into a 3-item list, RGB,
        # with individual values from 0-255
        color_string = str(item).lower()

        if color_string in NAMED_RGB_COLORS:
            return NAMED_RGB_COLORS[color_string]
        if Util.is_hex_string(color_string):
            return RGBColor.hex_to_rgb(color_string)

        color = Util.string_to_list(color_string)
        return int(color[0]), int(color[1]), int(color[2])
Example #4
0
    def _validate_type_color(self, item, validation_failure_info, param=None):
        assert not param

        if isinstance(item, tuple):
            if len(item) != 3:
                self.validation_error(item, validation_failure_info,
                                      "Color needs three components")
            return item

        # Validates colors by name, hex, or list, into a 3-item list, RGB,
        # with individual values from 0-255
        color_string = str(item)

        if color_string in NAMED_RGB_COLORS:
            return NAMED_RGB_COLORS[color_string]
        if Util.is_hex_string(color_string):
            return RGBColor.hex_to_rgb(color_string)

        color = Util.string_to_list(color_string)
        try:
            return int(color[0]), int(color[1]), int(color[2])
        except (IndexError, ValueError) as e:
            self.validation_error(item, validation_failure_info,
                                  "Could not parse color: {}".format(e))