Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Programming =>  Python

Python: calculate lighter/darker RGB colors

  Chase Seibert      2012-02-13 05:29:22      4,409    0    0

Many times color palettes have lighter and darker variations of the same color. This may be used to convey relative importance, or for something as simple as a gradient. Usually the designer will specify both colors. However, if you have a site that needs to allow user configurable styling, you may not want to ask the user for two variations of the same color.

Here is some Python code to take a single color in RGB, and output an artitrarily lighter or darker variation of the same color. You could wrap this in a filter and use it right in your Django templates.

  1. def color_variant(hex_color, brightness_offset=1):  
  2.     """ takes a color like #87c95f and produces a lighter or darker variant """  
  3.     if len(hex_color) != 7:  
  4.         raise Exception("Passed %s into color_variant(), needs to be in #87c95f format." % hex_color)  
  5.     rgb_hex = [hex_color[x:x+2for x in [135]]  
  6.     new_rgb_int = [int(hex_value, 16) + brightness_offset for hex_value in rgb_hex]  
  7.     new_rgb_int = [min([255, max([0, i])]) for i in new_rgb_int] # make sure new values are between 0 and 255  
  8.     # hex() produces "0x88", we want just "88"  
  9.     return "#" + "".join([hex(i)[2:] for i in new_rgb_int]) 

Source:http://bitkickers.blogspot.com/2011/07/python-calculate-lighterdarker-rgb.html

PYTHON RGC COLOR CALUCALTION LIGHTER/DARKER

  SAVE AS PDF   MARK AS READ   MARK AS IMPORTANT

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious

  RELATED


  0 COMMENT


No comment for this article.


  WRITE ARTICLE

Pair programming

By sonic0002