|
| 1 | + |
| 2 | +`Color` parses HTML and CSS colors. |
| 3 | + |
| 4 | +You can use the `Color` data type for storing colors as per |
| 5 | +[CSS3 specification](http://www.w3.org/TR/css3-color/#svg-color). Colors can be defined via: |
| 6 | + |
| 7 | +- [name](http://www.w3.org/TR/SVG11/types.html#ColorKeywords) (e.g. `"Black"`, `"azure"`) |
| 8 | +- [hexadecimal value](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet) |
| 9 | + (e.g. `"0x000"`, `"#FFFFFF"`, `"7fffd4"`) |
| 10 | +- RGB/RGBA tuples (e.g. `(255, 255, 255)`, `(255, 255, 255, 0.5)`) |
| 11 | +- [RGB/RGBA strings](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#RGB_colors) |
| 12 | + (e.g. `"rgb(255, 255, 255)"`, `"rgba(255, 255, 255, 0.5)"`) |
| 13 | +- [HSL strings](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#HSL_colors) |
| 14 | + (e.g. `"hsl(270, 60%, 70%)"`, `"hsl(270, 60%, 70%, .5)"`) |
| 15 | + |
| 16 | +```py |
| 17 | +from pydantic import BaseModel, ValidationError |
| 18 | + |
| 19 | +from pydantic_extra_types import Color |
| 20 | + |
| 21 | +c = Color('ff00ff') |
| 22 | +print(c.as_named()) |
| 23 | +#> magenta |
| 24 | +print(c.as_hex()) |
| 25 | +#> #f0f |
| 26 | +c2 = Color('green') |
| 27 | +print(c2.as_rgb_tuple()) |
| 28 | +#> (0, 128, 0) |
| 29 | +print(c2.original()) |
| 30 | +#> green |
| 31 | +print(repr(Color('hsl(180, 100%, 50%)'))) |
| 32 | +#> Color('cyan', rgb=(0, 255, 255)) |
| 33 | + |
| 34 | + |
| 35 | +class Model(BaseModel): |
| 36 | + color: Color |
| 37 | + |
| 38 | + |
| 39 | +print(Model(color='purple')) |
| 40 | +#> color=Color('purple', rgb=(128, 0, 128)) |
| 41 | +try: |
| 42 | + Model(color='hello') |
| 43 | +except ValidationError as e: |
| 44 | + print(e) |
| 45 | + """ |
| 46 | + 1 validation error for Model |
| 47 | + color |
| 48 | + value is not a valid color: string not recognised as a valid color [type=color_error, input_value='hello', input_type=str] |
| 49 | + """ |
| 50 | +``` |
| 51 | + |
| 52 | +`Color` has the following methods: |
| 53 | + |
| 54 | +**`original`** |
| 55 | +: the original string or tuple passed to `Color` |
| 56 | + |
| 57 | +**`as_named`** |
| 58 | +: returns a named CSS3 color; fails if the alpha channel is set or no such color exists unless |
| 59 | + `fallback=True` is supplied, in which case it falls back to `as_hex` |
| 60 | + |
| 61 | +**`as_hex`** |
| 62 | +: returns a string in the format `#fff` or `#ffffff`; will contain 4 (or 8) hex values if the alpha channel is set, |
| 63 | + e.g. `#7f33cc26` |
| 64 | + |
| 65 | +**`as_rgb`** |
| 66 | +: returns a string in the format `rgb(<red>, <green>, <blue>)`, or `rgba(<red>, <green>, <blue>, <alpha>)` |
| 67 | + if the alpha channel is set |
| 68 | + |
| 69 | +**`as_rgb_tuple`** |
| 70 | +: returns a 3- or 4-tuple in RGB(a) format. The `alpha` keyword argument can be used to define whether |
| 71 | + the alpha channel should be included; |
| 72 | + options: `True` - always include, `False` - never include, `None` (default) - include if set |
| 73 | + |
| 74 | +**`as_hsl`** |
| 75 | +: string in the format `hsl(<hue deg>, <saturation %>, <lightness %>)` |
| 76 | + or `hsl(<hue deg>, <saturation %>, <lightness %>, <alpha>)` if the alpha channel is set |
| 77 | + |
| 78 | +**`as_hsl_tuple`** |
| 79 | +: returns a 3- or 4-tuple in HSL(a) format. The `alpha` keyword argument can be used to define whether |
| 80 | + the alpha channel should be included; |
| 81 | + options: `True` - always include, `False` - never include, `None` (the default) - include if set |
| 82 | + |
| 83 | +The `__str__` method for `Color` returns `self.as_named(fallback=True)`. |
| 84 | + |
| 85 | +!!! note |
| 86 | + The `as_hsl*` refer to hue, saturation, lightness "HSL" as used in html and most of the world, **not** |
| 87 | + "HLS" as used in Python's `colorsys`. |
0 commit comments