🎨 nc
: Named colors in Python 🎨
The module nc
collects named colors in Python.
There are two ways to use nc
.
The simple way is as an intuitive and forgiving interface to a collection of over 2000 named colors, put together from almost 20 color palettes scraped from the Internet.
In the simplest use, it's a collection of about 1700 colors, some scraped from the Wikipedia (which includes some very strange colors), with a neat API.
For more precise use, color collections can be put together from schemes
built into nc
(currently html
, juce
, pwg
, wikipedia
, x11
),
or from custom color schemes created by the user.
There is also a collection of color swatches for the default color collection.
Examples
import nc
for c in nc.red, nc.light_green, nc.DarkGrey, nc['PUCE']:
print(c, '=', *c)
# Prints:
# Red = 255 0 0
# Light green = 144 238 144
# Dark grey = 85 85 85
# Puce = 204 136 153
# Colors have red, green, blue or r, g, b components
assert nc.yellow.red == nc.yellow.r == 0
assert nc.yellow.green == nc.yellow.g == 255
assert nc.yellow.blue == nc.yellow.b == 255
# Lots of ways to reach colors
assert nc.black == nc(0, 0, 0) == nc('0, 0, 0') == nc('(0, 0, 0)') == nc(0)
# ``nc`` looks like a dict
assert nc.red == nc['red'] == nc['RED']
for name, color in nc.items():
print(name, '=', *color)
# Prints:
# Absolute Zero = 0 72 186
# Acid green = 176 191 26
# Aero = 124 185 232
# ... many more
# closest() function
from random import randrange
for i in range(8):
c1 = randrange(256), randrange(256), randrange(256)
c2 = nc.closest(c1)
print(c1, 'is closest to', c2, *c2)
# Prints:
# (193, 207, 185) is closest to Honeydew 3 = 193 205 193
# (181, 162, 188) is closest to Lilac = 200 162 200
# (122, 110, 250) is closest to Slate blue 1 = 131 111 255
# (56, 218, 180) is closest to Turquoise = 64 224 208
API Documentation
NC
Source code in nc/__init__.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
COLORS: _colors.Colors
cached
property
The underlying instance of `nc.colors.Colors with all the colors
__contains__(x)
Return true if this string name, tuple or color is in this list
Source code in nc/__init__.py
105 106 107 |
|
__getattr__(name)
Gets a color as a name attribute
Source code in nc/__init__.py
89 90 91 92 93 94 95 |
|
__getitem__(name)
Gets a color by name
Source code in nc/__init__.py
101 102 103 |
|
__iter__()
Iterate over all the colors in alphabetical order
Source code in nc/__init__.py
113 114 115 |
|
__len__()
Returns the number of colors
Source code in nc/__init__.py
109 110 111 |
|
Color
Bases: COLOR_TUPLE
A single Color, represented as a named triple of integers in the range [0, 256).
Source code in nc/color.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
brightness: float
cached
property
gamma-weighted average of intensities
rgb: int
cached
property
Return an integer between 0 and 0xFFFFFF combining the components
closest()
Return the closest named color to self
. This is quite slow,
particularly in large schemes.
Source code in nc/color.py
39 40 41 42 43 44 |
|
distance(other)
Return the distance between this and another color
Source code in nc/color.py
51 52 53 |
|
distance2(other)
Return the square of the distance between this and another color
Source code in nc/color.py
46 47 48 49 |
|
Colors
A collection of named colors that acts like an array, a dictionary and a namespace.
Colors
is indexed by a type ColorKey
, which may be an
int
, slice
, str
, Color
, orTuple[int, int, int]]
.
Source code in nc/colors.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
__contains__(x)
Return true if this ColorKey appears in the table canonically
Source code in nc/colors.py
89 90 91 |
|
__getitem__(name)
Try to convert string item into a color
Source code in nc/colors.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
closest(color)
Return the closest named color to color
. This can be quite slow,
particularly if there are many colors.
Source code in nc/colors.py
57 58 59 60 61 62 63 64 65 66 |
|
get(key, default=None)
Return the value for key
if it exists, else default
.
Source code in nc/colors.py
38 39 40 41 42 43 |
|
items()
Return an iterator of name, Color pairs. Some colors might repeat
Source code in nc/colors.py
45 46 47 |
|
keys()
Return an iterator of strings, unique color names
Source code in nc/colors.py
53 54 55 |
|
values()
Return an iterator of Colors. Some colors might repeat
Source code in nc/colors.py
49 50 51 |
|