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 ¶
RGBA(r, g, b, alpha)
Internal use only as a representation of a color.
Source code in pydantic_extra_types/color.py
40 41 42 43 44 45 46 |
|
Color ¶
Color(value)
Bases: _repr.Representation
Represents a color.
Source code in pydantic_extra_types/color.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
original ¶
original()
Original value passed to Color
.
Source code in pydantic_extra_types/color.py
108 109 110 111 112 |
|
as_named ¶
as_named(*, fallback=False)
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 pydantic_extra_types/color.py
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 |
|
as_hex ¶
as_hex(format='short')
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 pydantic_extra_types/color.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
as_rgb ¶
as_rgb()
Color as an rgb(<r>, <g>, <b>)
or rgba(<r>, <g>, <b>, <a>)
string.
Source code in pydantic_extra_types/color.py
159 160 161 162 163 164 165 166 167 168 169 |
|
as_rgb_tuple ¶
as_rgb_tuple(*, alpha=None)
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 pydantic_extra_types/color.py
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 |
|
as_hsl ¶
as_hsl()
Color as an hsl(<h>, <s>, <l>)
or hsl(<h>, <s>, <l>, <a>)
string.
Source code in pydantic_extra_types/color.py
198 199 200 201 202 203 204 205 206 207 |
|
as_hsl_tuple ¶
as_hsl_tuple(*, alpha=None)
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 pydantic_extra_types/color.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
|
parse_tuple ¶
parse_tuple(value)
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 pydantic_extra_types/color.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
parse_str ¶
parse_str(value)
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 pydantic_extra_types/color.py
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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
|
ints_to_rgba ¶
ints_to_rgba(r, g, b, alpha=None)
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 pydantic_extra_types/color.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
|
parse_color_value ¶
parse_color_value(value, max_val=255)
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 pydantic_extra_types/color.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
|
parse_float_alpha ¶
parse_float_alpha(value)
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 pydantic_extra_types/color.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
parse_hsl ¶
parse_hsl(h, h_units, sat, light, alpha=None)
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 pydantic_extra_types/color.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
|
float_to_255 ¶
float_to_255(c)
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 pydantic_extra_types/color.py
479 480 481 482 483 484 485 486 487 488 489 |
|