Next / Previous / Contents / TCC Help System / NM Tech homepage

13.4. NamePicker.__setByName(): Try to convert a color name to a color

The purpose of this method is to convert a color name into a numeric value, and thence into a Color instance.

huey
# - - -   N a m e P i c k e r . _ _ s e t B y N a m e

    def __setByName ( self, colorName ):
        """Convert a color name to RGB values as a Color instance.

          [ colorName is a string ->
              if colorName is defined in either Tkinter or our
              color pick list ->
                call self.__callback and pass it that color as
                a Color instance
              else ->
                pop up a Dialog window complaining about the
                undefined color name ]
        """

In theory, a properly installed X Window system will have a file named rgb.txt that lists all the standard color names. In theory, any application should be able to use a color name from that file, and the window system will recognize it. In practice, the presence of that file is not guaranteed, and even if does exist, we have no assurance that the set of color names accepted by the window system is the same as the contents of the file.

In case the rgb.txt file is missing, this program contains a data structure which represents the contents of one version of that file.

However, there is another source for translating color names to RGB values. Every Tkinter widget has a method named .winfo_rgb() which takes a color name and returns a triple (R, G, B) where each value is in the range [0,MAX_PARAM].

So, our liberal policy will be to try the latter technique first. If that fails, we can still use the list that appears in the pick list to convert names to RGB values. That way, if the two sources differ, we implement the union of their values. There is a discussion of the Dialog pop-up in Section 12.4, “MenuBar.__helpTyping(). See also Section 14.3, “PickList.lookupName(): Convert a color name to a color value”.

huey
        #-- 1 --
        # [ if (Tkinter recognizes (colorName) as a color) or
        #   (colorName) is defined in self.__pickList) ->
        #     rgb  :=  that color as a tuple (r, g, b) of values
        #              in [0,MAX_PARAM]
        #   else ->
        #     pop up a Dialog complaining about the undefined
        #     color name
        #     return ]
        try:
            rgb  =  self.winfo_rgb ( colorName )
        except:
            rgb  =  self.__pickList.lookupName ( colorName )
            if  not rgb:
                Dialog ( self, title="Message", bitmap="info",
                    default=0, strings=("Dismiss",),
                    text=("Color '%s' is undefined." % colorName) )
                return

All that remains is to convert rgb to a Color and call the callback.

huey
        #-- 2 --
        # [ if self.__callback is not None ->
        #     call self.__callback with a Color made from rgb
        #   else -> I ]
        if  self.__callback is not None:
            self.__callback ( Color ( *rgb ) )