Next / Previous / TCC home / NMT home

Source listing for websafe.py

logo
#!/usr/local/bin/python
#--
# websafe.py: Show a palette of `web-safe colors'
#   $Revision: 1.1 $  $Date: 2001/05/27 22:34:51 $
#--

PROGRAM_NAME  =  "websafe.py 0.0"

import sys, string
from Tkinter import *
import tkFont

#================================================================
# Geometry of the swatches:
#
#               +-----------------------------------------+
#               | +---------+ +---------+ ... +---------+ |
#               | | #000000 | | #000033 |     | #0000ff | |
#               | +---------+ +---------+ ... +---------+ |
#               | | #003300 | | #003333 |     | #0033ff | |
#               | +---------+ +---------+ ... +---------+ |
#               / /   ...   / /         /     /  ...    / /
#               | +---------+ +---------+ ... +---------+ |      |
#               | | #00ff00 | | #00ff33 |     | #00ffff | |      v
#               | +---------+ +---------+ ... +---------+ |     ---
#               |                                         /    .__yGap
#               | +---------+ +---------+ ... +---------+ |     ---
#               | | #330000 | | #330033 |     | #3300ff | |      ^
#               | +---------+ +---------+ ... +---------+ |      |
#               / /         / /         /     /         / /
#
#                         ->| |<- .__xGap
#----------------------------------------------------------------


# - - - - -   c l a s s   A p p   - - - - -

class App(Frame):
    """Display a panel of web-safe colors.

        Materialize a Tkinter GUI panel showing the `web-safe
        colors,' that is, those of the form #rrggbb where each byte is in
        the set {0x00, 0x33, 0x66, 0x99, 0xcc, 0xff}.

        Contained widgets:
            .__canvas:  the Canvas on which the swatches are displayed
            .__quitButton:  a Quit button

        Other state:
            .__buttonFont:  Font for the quit button
            .__swatchFont:  Font for labeling color swatches

        Geometry:
            .__stepList:  List of color steps for each byte
            .__swatchHigh:  Height of each swatch
            .__xGap:  Gap between swatches horizontally
            .__swatchWide:  Width of each swatch
            .__yGap:  Gap between swatch groups vertically
                (swatches are adjacent in each group)               
    """


# - - -   A p p . _ _ i n i t _ _   - - -

    def __init__ ( self, master=None ):
        """Constructor for the app
        """
        # [ self  :=  a root Frame, gridded ]
        Frame.__init__(self, master)
        self.grid()

        # [ self  :=  self with geometric constants defined ]
        self.__stepList  =  [ "00", "33", "66", "99", "cc", "ff" ]
        self.__swatchHigh, self.__swatchWide  =   17, 80
        self.__xGap, self.__yGap  =  4,6
        self.__buttonFont  =  tkFont.Font ( family="Helvetica",
            size=24 )
        self.__swatchFont  =  tkFont.Font ( family="lucidatypewriter",
            size=-12 )

        # [ self  :=  self with all widgets created and gridded ]
        self.__createWidgets()

        # [ self.__canvas  :=  self.__canvas with all swatches rendered ]
        self.__createSwatches()


# - - -   A p p . _ _ c r e a t e W i d g e t s   - - -

    def __createWidgets(self):
        """Create these widgets within self:
        """
        rowx  =  0

        # [ self  :=  self with self.__canvas created and gridded ]
        nSteps  =  len ( self.__stepList )
        canvasWide  =  ( self.__xGap * ( nSteps + 1 ) + # Left margin
                         nSteps * self.__swatchWide )
        canvasHigh  =  ( self.__yGap * ( nSteps + 1 ) +     # Top & all gaps
                         nSteps * nSteps * self.__swatchHigh )
        self.__canvas  =  Canvas ( self,
            bg="gray85", 
            height=canvasHigh, width=canvasWide )
        self.__canvas.grid ( row=rowx, column=0 )
        rowx  =  rowx + 1

        # [ self.__quitButton  :=  a new button that quits ]
        self.__quitButton  =  Button ( self, text="Quit",
            font=self.__buttonFont,
            fg="coral", bg="black", command=self.quit )
        self.__quitButton.grid ( row=rowx, column=0, pady=5, 
            sticky=E+W )
        rowx  =  rowx + 1


# - - -   A p p . _ _ c r e a t e S w a t c h e s   - - -

    def __createSwatches(self):
        """Display the swatches and their labels on self.__canvas
        """
        nSteps  =  len(self.__stepList)
        for redx in range(nSteps):
            for greenx in range(nSteps):
                for bluex in range(nSteps):
                    # [ self.__canvas  :=  self.__canvas with swatch
                    #       rendered for (red+green+blue) ]
                    self.__renderSwatch ( redx, greenx, bluex )


# - - -   A p p . _ _ r e n d e r S w a t c h   - - -

    def __renderSwatch ( self, redx, greenx, bluex ):
        """Render one color swatch and its label.
        """
        # [ x, y    :=  coordinates of top left corner of swatch
        #   nSteps  :=  len(self.__stepList) ]
        nSteps  =  len(self.__stepList)
        x  =  ( self.__xGap +                   # Left margin
                bluex * ( self.__swatchWide + self.__xGap ) )
        y  =  ( self.__yGap +                   # Top margin
                self.__swatchHigh * greenx +    # Offset within group
                redx * ( self.__yGap + nSteps * self.__swatchHigh ) )

        # [ self.__canvas  +:=  a canvas rectangle of width
        #       self.__swatchWide, height self.__swatchHigh, and
        #       fill color selected from self.__stepList by redx,   
        #       greenx, and bluex ]
        fillColor  =  ( "#%s%s%s" %
                        ( self.__stepList[redx], self.__stepList[greenx],
                          self.__stepList[bluex] ) )
        self.__canvas.create_rectangle ( x, y,
            x + self.__swatchWide, y + self.__swatchHigh,
            outline=fillColor,
            width=0,     # No border
            fill=fillColor )

        if (redx+greenx+bluex) < (nSteps*3/2.2):
            textColor  =  "white"
        else:
            textColor  =  "black"

        # [ self.__canvas  +:=  label showing fillcolor within swatch ]
        self.__canvas.create_text ( x+2, y+2,
            text=fillColor,
            anchor=NW, fill=textColor, font=self.__swatchFont )


# - - - - -   m a i n   - - - - -

app = App()
app.master.title(PROGRAM_NAME)
app.mainloop()

TCC home: TCC home
NMT home: NMT home

Last updated: 2014-09-30 08:00 MDT