color.Color
- class color.Color(red: float, green: float, blue: float)
A class that holds the RGB values for a color as floats from 0 to 1. Default colors are included matching the HTML 4.01 specification.
- r
The red component of the color
from color import Color mycolor = Color.maroon() print(f'red component: {mycolor.r}')
- g
The green component of the color
from color import Color mycolor = Color(0,.1,.0) print(f'green component: {mycolor.g}')
- b
The blue component of the color
from color import Color mycolor = Color.from_hex(#0000F1) print(f'blue component: {mycolor.b : 0.1}')
- static black()
#000000
- static silver()
#c0c0c0
- static gray()
#808080
- static white()
#ffffff
- static maroon()
#800000
- static red()
#ff0000
- static purple()
#800080
- static fuchsia()
#ff00ff
- static green()
#008000
- static lime()
#00ff00
- static olive()
#808000
- static yellow()
#ffff00
#000080
- static blue()
#0000ff
- static teal()
#008080
- static aqua()
#00ffff
- to_hex() str
Converts color object into hexidecimal string preceeded by #.
from color import Color mycolor = Color(1,0,0) print(f'color in hex: {mycolor.to_hex()}') # prints 'color in hex: #FF0000'
- static from_hex(hex_code: str) Optional[Color]
Returns a color object corresponding to
hex_code
.hex_code
should be in the form “#RRGGBB” where red would be #ff0000. hex_code can also be supplied as a preset color name matching one of the color properties inColor
.from color import Color # the word teal comes from the name of a kind # of duck which has some teal coloring on its head teals = [ Color(0,128/255,128/255), Color.from_hex('#008080'), Color.from_hex('teal'), Color.teal() ] for i in range(0,len(teals)): print(f'teal {i}: {teals[i]: 0.2f}')
- hue() float
Returns the hue of the color.
from color import Color my_color = Color.from_hex('#009900') print(f'Hue of #009900: {my_color.hue(): 0.1f} degrees')
- saturation()
Returns the saturation of the color.
from color import Color my_color = Color.from_hex('#009900') print(f'Saturation of #009900: {my_color.saturation() * 100: 0.1f}%')
- value() float
Returns the value of the color.
from color import Color my_color = Color.from_hex('#009900') print(f'Value of #009900: {my_color.value() * 100: 0.1f}%')
- distance(color2: Color) float
Returns the euclidean distance between the colors.
Note
There are many ways to measure the distance between colors that take different factors into account. This is just a simple starting point.
from color import Color distance = Color.maroon().distance(Color.navy()) print(f'|maroon - navy| = {distance: 0.2f}')