def test_round_trip_maximum(self):
     """ Test to assert stability of values through round-trips """
     for value in range(65536):
         with self.subTest(int=value):
             result = channels_to_ints(
                 ints_to_channels(
                     [value],
                     maximum=65535,
                 ),
                 maximum=65535,
             )
             self.assertEqual(result, (value, ))
Exemplo n.º 2
0
    def hex(self):
        """ Provide a hex representation of the Color object.

        Note that because the hex value is restricted to 0-255 integer values
        for each channel, the representation is not exact.

        Returns
        -------
        hex : str
            A hex string in standard ``#RRGGBBAA`` format that represents
            the color.
        """
        values = channels_to_ints(self.rgba)
        return "#{:02X}{:02X}{:02X}{:02X}".format(*values)
Exemplo n.º 3
0
def convert_to_color(object, name, value):
    """Converts a number into a QColor object."""
    # Try the toolkit agnostic format.
    try:
        tup = literal_eval(value)
    except Exception:
        tup = value

    if isinstance(value, str):
        # Allow for spaces in the string value.
        value = value.replace(" ", "")

        # is it in the color table?
        if value in color_table:
            tup = channels_to_ints(color_table[value])

    if isinstance(tup, tuple):
        if 3 <= len(tup) <= 4 and all(isinstance(x, int) for x in tup):
            try:
                color = QtGui.QColor(*tup)
            except Exception:
                raise TraitError
        else:
            raise TraitError
    elif isinstance(value, PyfaceColor):
        color = value.to_toolkit()
    else:
        # Let the standard ctors handle the value.
        try:
            color = QtGui.QColor(value)
        except TypeError:
            raise TraitError

    if not color.isValid():
        raise TraitError

    return color
Exemplo n.º 4
0
    def __init__(self):
        # correct for differences in definitions
        self._color_names = [
            "aqua",
            "black",
            "blue",
            "fuchsia",
            "gray",
            "green",
            "lime",
            "maroon",
            "navy",
            "olive",
            "purple",
            "red",
            "silver",
            "teal",
            "white",
            "yellow",
        ]

        self.AddColour("aqua", wx.Colour(0, 0xFF, 0xFF, 255))
        self.AddColour("fuchsia", wx.Colour(0xFF, 0, 0xFF, 255))
        self.AddColour("green", wx.Colour(0, 0x80, 0, 255))
        self.AddColour("lime", wx.Colour(0, 0xFF, 0, 255))
        self.AddColour("maroon", wx.Colour(0x80, 0x0, 0, 255))
        self.AddColour("navy", wx.Colour(0x00, 0x0, 0x80, 255))
        self.AddColour("olive", wx.Colour(0x80, 0x80, 0, 255))
        self.AddColour("purple", wx.Colour(0x80, 0x00, 0x80, 255))
        self.AddColour("silver", wx.Colour(0xC0, 0xC0, 0xC0, 255))
        self.AddColour("teal", wx.Colour(0, 0x80, 0x80, 255))

        # add all the standard colours
        for name, rgba in color_table.items():
            rgba_bytes = channels_to_ints(rgba)
            self.AddColour(name, wx.Colour(*rgba_bytes))
 def test_channels_to_ints_maximum(self):
     channels = (0.4, 0.4, 0.0, 1.0)
     values = channels_to_ints(channels, maximum=15)
     self.assertEqual(values, (6, 6, 0, 15))
 def test_channels_to_ints(self):
     channels = (0.4, 0.4, 0.0, 1.0)
     values = channels_to_ints(channels)
     self.assertEqual(values, (102, 102, 0, 255))
Exemplo n.º 7
0
    return color


convert_to_color.info = ("a string of the form (r,g,b) or (r,g,b,a) where r, "
                         "g, b, and a are integers from 0 to 255, a QColor "
                         "instance, a Qt.GlobalColor, an integer which in hex "
                         "is of the form 0xRRGGBB, a string of the form #RGB, "
                         "#RRGGBB, #RRRGGGBBB or #RRRRGGGGBBBB")

# -------------------------------------------------------------------------
#  Standard colors:
# -------------------------------------------------------------------------

standard_colors = {}
for name, rgba in color_table.items():
    rgba_bytes = channels_to_ints(rgba)
    standard_colors[str(name)] = QtGui.QColor(*rgba_bytes)

# -------------------------------------------------------------------------
#  Callable that returns an instance of the PyQtToolkitEditorFactory for color
#  editors.
# -------------------------------------------------------------------------

### FIXME: We have declared the 'editor' to be a function instead of  the
# traitsui.qt4.color_editor.ToolkitEditorFactory class, since the
# latter is leading to too many circular imports. In the future, try to see if
# there is a better way to do this.


def get_color_editor(*args, **traits):
    from traitsui.qt4.color_editor import ToolkitEditorFactory