2013-10-12 09:54:24 +04:00
|
|
|
.. py:module:: PIL.ImageColor
|
|
|
|
.. py:currentmodule:: PIL.ImageColor
|
|
|
|
|
2020-06-22 06:52:50 +03:00
|
|
|
:py:mod:`~PIL.ImageColor` Module
|
|
|
|
================================
|
2013-10-12 09:54:24 +04:00
|
|
|
|
2020-06-22 06:52:50 +03:00
|
|
|
The :py:mod:`~PIL.ImageColor` module contains color tables and converters from
|
2013-10-12 09:54:24 +04:00
|
|
|
CSS3-style color specifiers to RGB tuples. This module is used by
|
2018-02-14 12:09:00 +03:00
|
|
|
:py:meth:`PIL.Image.new` and the :py:mod:`~PIL.ImageDraw` module, among
|
2013-10-12 09:54:24 +04:00
|
|
|
others.
|
|
|
|
|
2013-10-12 11:52:01 +04:00
|
|
|
.. _color-names:
|
|
|
|
|
2013-10-12 09:54:24 +04:00
|
|
|
Color Names
|
|
|
|
-----------
|
|
|
|
|
|
|
|
The ImageColor module supports the following string formats:
|
|
|
|
|
2021-01-07 16:06:18 +03:00
|
|
|
* Hexadecimal color specifiers, given as ``#rgb``, ``#rgba``, ``#rrggbb`` or
|
2021-02-09 11:47:40 +03:00
|
|
|
``#rrggbbaa``, where ``r`` is red, ``g`` is green, ``b`` is blue and ``a`` is
|
|
|
|
alpha (also called 'opacity'). For example, ``#ff0000`` specifies pure red,
|
|
|
|
and ``#ff0000cc`` specifies red with 80% opacity (``cc`` is 204 in decimal
|
|
|
|
form, and 204 / 255 = 0.8).
|
2013-10-12 09:54:24 +04:00
|
|
|
|
|
|
|
* RGB functions, given as ``rgb(red, green, blue)`` where the color values are
|
|
|
|
integers in the range 0 to 255. Alternatively, the color values can be given
|
|
|
|
as three percentages (0% to 100%). For example, ``rgb(255,0,0)`` and
|
|
|
|
``rgb(100%,0%,0%)`` both specify pure red.
|
|
|
|
|
|
|
|
* Hue-Saturation-Lightness (HSL) functions, given as ``hsl(hue, saturation%,
|
|
|
|
lightness%)`` where hue is the color given as an angle between 0 and 360
|
|
|
|
(red=0, green=120, blue=240), saturation is a value between 0% and 100%
|
|
|
|
(gray=0%, full color=100%), and lightness is a value between 0% and 100%
|
|
|
|
(black=0%, normal=50%, white=100%). For example, ``hsl(0,100%,50%)`` is pure
|
|
|
|
red.
|
|
|
|
|
2018-05-30 23:13:22 +03:00
|
|
|
* Hue-Saturation-Value (HSV) functions, given as ``hsv(hue, saturation%,
|
|
|
|
value%)`` where hue and saturation are the same as HSL, and value is between
|
|
|
|
0% and 100% (black=0%, normal=100%). For example, ``hsv(0,100%,100%)`` is
|
2018-05-30 23:14:29 +03:00
|
|
|
pure red. This format is also known as Hue-Saturation-Brightness (HSB), and
|
|
|
|
can be given as ``hsb(hue, saturation%, brightness%)``, where each of the
|
|
|
|
values are used as they are in HSV.
|
2018-05-30 23:13:22 +03:00
|
|
|
|
2013-10-12 09:54:24 +04:00
|
|
|
* Common HTML color names. The :py:mod:`~PIL.ImageColor` module provides some
|
|
|
|
140 standard color names, based on the colors supported by the X Window
|
|
|
|
system and most web browsers. color names are case insensitive. For example,
|
|
|
|
``red`` and ``Red`` both specify pure red.
|
|
|
|
|
|
|
|
Functions
|
|
|
|
---------
|
|
|
|
|
2017-12-30 13:23:04 +03:00
|
|
|
.. py:method:: getrgb(color)
|
|
|
|
|
|
|
|
Convert a color string to an RGB tuple. If the string cannot be parsed,
|
|
|
|
this function raises a :py:exc:`ValueError` exception.
|
|
|
|
|
|
|
|
.. versionadded:: 1.1.4
|
|
|
|
|
|
|
|
.. py:method:: getcolor(color, mode)
|
|
|
|
|
|
|
|
Same as :py:func:`~PIL.ImageColor.getrgb`, but converts the RGB value to a
|
2023-10-19 11:12:01 +03:00
|
|
|
grayscale value if the mode is not color or a palette image. If the string
|
2017-12-30 13:23:04 +03:00
|
|
|
cannot be parsed, this function raises a :py:exc:`ValueError` exception.
|
|
|
|
|
|
|
|
.. versionadded:: 1.1.4
|