File: quickstart.rst

package info (click to toggle)
python-colormap 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 760 kB
  • sloc: python: 1,115; makefile: 114; javascript: 44
file content (227 lines) | stat: -rw-r--r-- 5,535 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
.. _quickstart:

Conventions
==============

hexadecimal
---------------
hexadecimal can be encoded as explained in :meth:`colormap.colors.hex2rgb`:

* #FFF
* #0000FF
* 0x0000FF
* 0xFA1

normalisation
---------------

By default, input should be normalised (e.g., RGB values between 0 and 1) and outputs are normalised.
If you provide unnormalised values (e.g., RGB in 0-255) then set the noramlised
parameter to True (see example in codecs).


Codecs
==========

list
--------

There is a bunch of codecs available in :mod:`colormap.colors` such as
hex2rgb::


    >>> from colormap.colors import hex2rgb
    >>> hex2rgb("#FFF", normalise=False)
    (255, 255, 255)
    >>> hex2rgb("#FFFFFF", normalise=True)
    (1.0, 1.0, 1.0)

=============== =====================================
codecs
=============== =====================================
hex2web         :meth:`colormap.colors.hex2web`
web2hex         :meth:`colormap.colors.web2hex`
hex2rgb         :meth:`colormap.colors.hex2rgb`
rgb2hex         :meth:`colormap.colors.rgb2hex`
rgb2hls         :meth:`colormap.colors.rgb2hls`
rgb2hsv         :meth:`colormap.colors.rgb2hsv`
hsv2rgb         :meth:`colormap.colors.hsv2rgb`
hls2rgb         :meth:`colormap.colors.hls2rgb`
hex2dec         :meth:`colormap.colors.hex2dec`
yuv2rgb         :meth:`colormap.colors.yuv2rgb`
rgb2yuv_int     :meth:`colormap.colors.rgb2yuv_int`
yuv2rgb_int     :meth:`colormap.colors.yuv2rgb_int`
=============== =====================================

format
----------

* RGB (red/green/blue): a triple of values between 0 and 255
* HLS (): H in 0-360 and L,S in 0-100
* HSV (): H in 0-360, S,V in
* YUV: all in 0-1

Color class
===========

On task, which is quite common is to know the hexadecimal code of a color known
by name (e.g. red). The :class:`colormap.colors.Color` would be useful::


    >>> c = Color('red')
    >>> c.rgb
    (1.0, 0.0, 0.0)
    >>> c.hls
    (0.0, 0.5, 1.0)
    >>> c.hex
    '#FF0000'

    >>> print(c)
    Color Red
    hexa code: #FF0000
    RGB code: (1.0, 0.0, 0.0)
    RGB code (un-normalised): [255.0, 0.0, 0.0]

    HSV code: (0.0, 1.0, 1.0)
    HSV code: (un-normalised) 0.0 100.0 100.0

    HLS code: (0.0, 0.5, 1.0)
    HLS code: (un-normalised) 0.0 50.0 100.0

Input when instanciating can be anything in RGB, HEX, HLS, common name from
:mod:`colormap.xfree86`::

    >>> sorted(colormap.xfree86.XFree86_colors.keys())


colormap
============

There are lots of colormaps in matplotlib. This is great but some may be missing
or it is not obvious to know what the colormap will look like.

The :class:`colormap.colors.Colormap` class allows you:

- To build easily new colormaps and visualise them
- Visualise existing colormaps

visualise colormaps
-------------------------

.. plot::
    :include-source:
    :width: 80%

    >>> from colormap import plot_category
    >>> plot_categoryp('sequentials')


.. plot::
    :include-source:
    :width: 80%

    >>> from colormap import plot_category
    >>> plot_category('sequentials2')


.. plot::
    :include-source:
    :width: 80%

    >>> from colormap import plot_category
    >>> plot_category('misc')


.. plot::
    :include-source:
    :width: 80%

    >>> from colormap import plot_category
    >>> plot_category('diverging')


.. plot::
    :include-source:
    :width: 80%

    >>> from colormap import plot_category
    >>> plot_category('qualitative')


Visualise existing colormap
-----------------------------

.. plot::
    :include-source:
    :width: 80%

    >>> from colormap import plot_colormap,
    >>> plot_colormap("viridis")




Create a linear colormap
-------------------------------

The simplest colormap are linear with 3 colors. In such case, we provide a
method that is easy to use. Imagine you want a colormap from red to green with
white color in between:

.. plot::
   :include-source:
   :width: 80%

   from colormap import Colormap
   c = Colormap()
   cmap = c.cmap_linear('red', 'white', 'green')
   cmap = c.test_colormap(cmap)



Here, we use color names, which are the xfree86 names. However, you could have
used any format accepted by :class:`~colormap.Colors`::

    red = Color('red')
    cmap = cmap_linear(red, 'white', '#0000FF')

Create a general colormap
-----------------------------

In the previous example, we used 3 colors assuming a linear scale. However, you
may want a different scale, in which case, you need to provide more colors. In
such case, you can use :meth:`~colormap.colors.Colormap.cmap` method.

Here we again use the same example a above but it can be generalised easily.
First, we need to know the RGB components of the colors::

    >>> from colormap import Color, Colormap
    >>> green = Color('Dark Green').rgb
    >>> red = Color('red').rgb
    >>> white = Color('white').rgb
    >>> white
    (1.0, 1.0, 1.0)


For instance RGB values of white are 1,1,1

Second, built a dictionary with the three RGB name (red/green/blue) as keys and with the values being the
evolution of the red/green/blue when a value goes from 0 to 1. Here, we use a
linear scaling so we just need 3 values at 0, 0.5, and 1. Therefore we have list of 4 values.
You could provide list of arbitrary lengths if required ::

    >>> c = Colormap()
    >>> mycmap = c.cmap( {'red':[1,1,0,1], 'green':[0,1,.39,1],  'blue':[0,1,0,1]})


Finally, test it::

    c.test_colormap(mycmap)


.. plot::

   from colormap import Colormap
   c = Colormap()
   c.test_colormap(c.cmap({'red':[1,1,0,1], 'green':[0,1,.39,1],
                          'blue':[0,1,0,1]}))