[pygtk] Generating theme-compatible colors

Samuel Abels newsgroups at debain.org
Fri Feb 20 03:00:11 WST 2009


In case anybody has the same problem, I solved it by creating a static
list of colors (taken from the Tango specs), and choosing the text color
depending on the sum of the color values. The code is here:

http://code.google.com/p/spiff-gtkwidgets/source/browse/trunk/src/SpiffGtkWidgets/color.py

(look at the from_string function in that file)

It still has the drawback that you can only get three related colors,
but that's probably enough in most cases.

-Samuel

On Tue, 2009-02-17 at 01:12 +0100, Samuel Abels wrote:
> Hi,
> 
> I am looking for a way to automatically get a set of related colors that
> go well with the current theme. Because multiple sets of colors are
> needed, I can't simply use the theme color.
> 
> (Explanation: I am writing an application in which colored boxes are
> added into a text field, and the colors of each box are picked
> automatically depending on the user who added them.)
> 
> Has anyone done this before me? My current (braindead) approach looks
> like this:
> 
> palette = [(0.12, 0.29, 0.49),
>            ...
>            (1.0,  0.90, 0.34)]
> def from_string(string, n_colors = 3): 
>     first = string.__hash__() % (len(palette) - n_colors)
>     if n_colors == 1:
>         return palette[first]
>     return [palette[i] for i in range(first, first + n_colors)]
> 
> This somewhat works, but the colors often don't go well with each other,
> and a white box may be invisible on a theme in which textview widgets
> have a white background.
> 
> Any idea how to do this properly?
> 
> -Samuel
> 
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/



More information about the pygtk mailing list