FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib/examples/colours.py at transforms · tecki/matplotlib · GitHub
tecki
matplotlib
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib
/
examples
/
colours.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
50 lines (40 loc) · 1.28 KB
Breadcrumbs
matplotlib
/
examples
/
colours.py
Copy path
File metadata and controls
50 lines (40 loc) · 1.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python
"""
Some simple functions to generate colours.
"""
import
numpy
as
npy
from
matplotlib
.
colors
import
colorConverter
def
pastel
(
colour
,
weight
=
2.4
):
""" Convert colour into a nice pastel shade"""
rgb
=
npy
.
asarray
(
colorConverter
.
to_rgb
(
colour
))
# scale colour
maxc
=
max
(
rgb
)
if
maxc
<
1.0
and
maxc
>
0
:
# scale colour
scale
=
1.0
/
maxc
rgb
=
rgb
*
scale
# now decrease saturation
total
=
rgb
.
sum
()
slack
=
0
for
x
in
rgb
:
slack
+=
1.0
-
x
# want to increase weight from total to weight
# pick x s.t. slack * x == weight - total
# x = (weight - total) / slack
x
=
(
weight
-
total
)
/
slack
rgb
=
[
c
+
(
x
*
(
1.0
-
c
))
for
c
in
rgb
]
return
rgb
def
get_colours
(
n
):
""" Return n pastel colours. """
base
=
npy
.
asarray
([[
1
,
0
,
0
], [
0
,
1
,
0
], [
0
,
0
,
1
]])
if
n
<=
3
:
return
base
[
0
:
n
]
# how many new colours to we need to insert between
# red and green and between green and blue?
needed
=
(((
n
-
3
)
+
1
)
/
2
, (
n
-
3
)
/
2
)
colours
=
[]
for
start
in
(
0
,
1
):
for
x
in
npy
.
linspace
(
0
,
1
,
needed
[
start
]
+
2
):
colours
.
append
((
base
[
start
]
*
(
1.0
-
x
))
+
(
base
[
start
+
1
]
*
x
))
return
[
pastel
(
c
)
for
c
in
colours
[
0
:
n
]]
Back
|
FazBrowse Home
|
New Git URL