Color
Color definitions are used as per the CSS3 CSS Color Module Level 3 specification.
A few colors have multiple names referring to the sames colors, eg. grey
and gray
or aqua
and cyan
.
In these cases the last color when sorted alphabetically takes preferences,
eg. Color((0, 255, 255)).as_named() == 'cyan'
because "cyan" comes after "aqua".
RGBA ¶
Internal use only as a representation of a color.
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
35 36 37 38 39 40 41 |
|
Color ¶
Color(value: ColorType)
Bases: Representation
Represents a color.
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
original ¶
original() -> ColorType
Original value passed to Color
.
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
103 104 105 106 107 |
|
as_named ¶
Returns the name of the color if it can be found in COLORS_BY_VALUE
dictionary,
otherwise returns the hexadecimal representation of the color or raises ValueError
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fallback |
bool
|
If True, falls back to returning the hexadecimal representation of the color instead of raising a ValueError when no named color is found. |
False
|
Returns:
Type | Description |
---|---|
str
|
The name of the color, or the hexadecimal representation of the color. |
Raises:
Type | Description |
---|---|
ValueError
|
When no named color is found and fallback is |
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
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 |
|
as_hex ¶
Returns the hexadecimal representation of the color.
Hex string representing the color can be 3, 4, 6, or 8 characters depending on whether the string a "short" representation of the color is possible and whether there's an alpha channel.
Returns:
Type | Description |
---|---|
str
|
The hexadecimal representation of the color. |
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
as_rgb ¶
as_rgb() -> str
Color as an rgb(<r>, <g>, <b>)
or rgba(<r>, <g>, <b>, <a>)
string.
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
154 155 156 157 158 159 160 161 162 163 164 |
|
as_rgb_tuple ¶
as_rgb_tuple(*, alpha: bool | None = None) -> ColorTuple
Returns the color as an RGB or RGBA tuple.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
bool | None
|
Whether to include the alpha channel. There are three options for this input:
|
None
|
Returns:
Type | Description |
---|---|
ColorTuple
|
A tuple that contains the values of the red, green, and blue channels in the range 0 to 255. If alpha is included, it is in the range 0 to 1. |
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
as_hsl ¶
as_hsl() -> str
Color as an hsl(<h>, <s>, <l>)
or hsl(<h>, <s>, <l>, <a>)
string.
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
187 188 189 190 191 192 193 194 195 196 |
|
as_hsl_tuple ¶
as_hsl_tuple(*, alpha: bool | None = None) -> HslColorTuple
Returns the color as an HSL or HSLA tuple.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
bool | None
|
Whether to include the alpha channel.
|
None
|
Returns:
Type | Description |
---|---|
HslColorTuple
|
The color as a tuple of hue, saturation, lightness, and alpha (if included). All elements are in the range 0 to 1. |
Note
This is HSL as used in HTML and most other places, not HLS as used in Python's colorsys
.
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
parse_tuple ¶
Parse a tuple or list to get RGBA values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
tuple[Any, ...]
|
A tuple or list. |
required |
Returns:
Type | Description |
---|---|
RGBA
|
An |
Raises:
Type | Description |
---|---|
PydanticCustomError
|
If tuple is not valid. |
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|
parse_str ¶
Parse a string representing a color to an RGBA tuple.
Possible formats for the input string include:
- named color, see
COLORS_BY_NAME
- hex short eg.
<prefix>fff
(prefix can be#
,0x
or nothing) - hex long eg.
<prefix>ffffff
(prefix can be#
,0x
or nothing) rgb(<r>, <g>, <b>)
rgba(<r>, <g>, <b>, <a>)
transparent
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
str
|
A string representing a color. |
required |
Returns:
Type | Description |
---|---|
RGBA
|
An |
Raises:
Type | Description |
---|---|
ValueError
|
If the input string cannot be parsed to an RGBA tuple. |
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|
ints_to_rgba ¶
Converts integer or string values for RGB color and an optional alpha value to an RGBA
object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r |
int | str
|
An integer or string representing the red color value. |
required |
g |
int | str
|
An integer or string representing the green color value. |
required |
b |
int | str
|
An integer or string representing the blue color value. |
required |
alpha |
float | None
|
A float representing the alpha value. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
RGBA
|
An instance of the |
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
|
parse_color_value ¶
Parse the color value provided and return a number between 0 and 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
int | str
|
An integer or string color value. |
required |
max_val |
int
|
Maximum range value. Defaults to 255. |
255
|
Raises:
Type | Description |
---|---|
PydanticCustomError
|
If the value is not a valid color. |
Returns:
Type | Description |
---|---|
float
|
A number between 0 and 1. |
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
|
parse_float_alpha ¶
Parse an alpha value checking it's a valid float in the range 0 to 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
None | str | float | int
|
The input value to parse. |
required |
Returns:
Type | Description |
---|---|
float | None
|
The parsed value as a float, or |
Raises:
Type | Description |
---|---|
PydanticCustomError
|
If the input value cannot be successfully parsed as a float in the expected range. |
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
|
parse_hsl ¶
Parse raw hue, saturation, lightness, and alpha values and convert to RGBA.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h |
str
|
The hue value. |
required |
h_units |
str
|
The unit for hue value. |
required |
sat |
str
|
The saturation value. |
required |
light |
str
|
The lightness value. |
required |
alpha |
float | None
|
Alpha value. |
None
|
Returns:
Type | Description |
---|---|
RGBA
|
An instance of |
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
|
float_to_255 ¶
Converts a float value between 0 and 1 (inclusive) to an integer between 0 and 255 (inclusive).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c |
float
|
The float value to be converted. Must be between 0 and 1 (inclusive). |
required |
Returns:
Type | Description |
---|---|
int
|
The integer equivalent of the given float value rounded to the nearest whole number. |
Source code in .venv/lib/python3.12/site-packages/pydantic_extra_types/color.py
456 457 458 459 460 461 462 463 464 465 466 |
|