Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
word_cloud_est
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
keeleliin
word_cloud_est
Commits
39369d22
Commit
39369d22
authored
Jan 20, 2016
by
Raphael Boidol
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add single color generating function
parent
df20defd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
3 deletions
+66
-3
test/test_wordcloud.py
test/test_wordcloud.py
+31
-1
wordcloud/__init__.py
wordcloud/__init__.py
+2
-2
wordcloud/wordcloud.py
wordcloud/wordcloud.py
+33
-0
No files found.
test/test_wordcloud.py
View file @
39369d22
from
wordcloud
import
WordCloud
from
wordcloud
import
WordCloud
,
get_single_color_func
import
numpy
as
np
from
random
import
Random
from
nose.tools
import
assert_equal
,
assert_greater
,
assert_true
,
assert_raises
from
numpy.testing
import
assert_array_equal
from
PIL
import
Image
...
...
@@ -131,6 +132,35 @@ def test_mask():
assert_greater
(
wc_array
[
mask
==
0
].
sum
(),
10000
)
def
test_single_color_func
():
# test single color function for different color formats
random
=
Random
(
42
)
red_function
=
get_single_color_func
(
'red'
)
assert_equal
(
red_function
(
random_state
=
random
),
'rgb(181, 0, 0)'
)
hex_function
=
get_single_color_func
(
'#00b4d2'
)
assert_equal
(
hex_function
(
random_state
=
random
),
'rgb(0, 48, 56)'
)
rgb_function
=
get_single_color_func
(
'rgb(0,255,0)'
)
assert_equal
(
rgb_function
(
random_state
=
random
),
'rgb(0, 107, 0)'
)
rgb_perc_fun
=
get_single_color_func
(
'rgb(80%,60%,40%)'
)
assert_equal
(
rgb_perc_fun
(
random_state
=
random
),
'rgb(97, 72, 48)'
)
hsl_function
=
get_single_color_func
(
'hsl(0,100%,50%)'
)
assert_equal
(
hsl_function
(
random_state
=
random
),
'rgb(201, 0, 0)'
)
def
test_single_color_func_grey
():
# grey is special as it's a corner case
random
=
Random
(
42
)
red_function
=
get_single_color_func
(
'darkgrey'
)
assert_equal
(
red_function
(
random_state
=
random
),
'rgb(181, 181, 181)'
)
assert_equal
(
red_function
(
random_state
=
random
),
'rgb(56, 56, 56)'
)
def
check_parameters
():
# check that parameters are actually used
pass
wordcloud/__init__.py
View file @
39369d22
from
.wordcloud
import
WordCloud
,
STOPWORDS
,
random_color_func
from
.wordcloud
import
WordCloud
,
STOPWORDS
,
random_color_func
,
get_single_color_func
from
.color_from_image
import
ImageColorGenerator
__all__
=
[
'WordCloud'
,
'STOPWORDS'
,
'random_color_func'
,
'ImageColorGenerator'
]
__all__
=
[
'WordCloud'
,
'STOPWORDS'
,
'random_color_func'
,
'
get_single_color_func'
,
'
ImageColorGenerator'
]
wordcloud/wordcloud.py
View file @
39369d22
...
...
@@ -10,10 +10,12 @@ from random import Random
import
os
import
re
import
sys
import
colorsys
import
numpy
as
np
from
operator
import
itemgetter
from
PIL
import
Image
from
PIL
import
ImageColor
from
PIL
import
ImageDraw
from
PIL
import
ImageFont
...
...
@@ -77,6 +79,37 @@ def random_color_func(word=None, font_size=None, position=None,
random_state
=
Random
()
return
"hsl(%d, 80%%, 50%%)"
%
random_state
.
randint
(
0
,
255
)
def
get_single_color_func
(
color
):
"""Create a color function which returns a single hue and saturation with.
different values (HSV). Accepted values are color strings as usable by PIL/Pillow.
>>> color_func1 = get_single_color_func('deepskyblue')
>>> color_func2 = get_single_color_func('#00b4d2')
"""
old_r
,
old_g
,
old_b
=
ImageColor
.
getrgb
(
color
)
rgb_max
=
255.
h
,
s
,
v
=
colorsys
.
rgb_to_hsv
(
old_r
/
rgb_max
,
old_g
/
rgb_max
,
old_b
/
rgb_max
)
def
single_color_func
(
word
=
None
,
font_size
=
None
,
position
=
None
,
orientation
=
None
,
font_path
=
None
,
random_state
=
None
):
"""Random color generation.
Additional coloring method. It picks a random value with hue and
saturation based on the color given to the generating function.
Parameters
----------
word, font_size, position, orientation : ignored.
random_state : random.Random object or None, (default=None)
If a random object is given, this is used for generating random numbers.
"""
if
random_state
is
None
:
random_state
=
Random
()
r
,
g
,
b
=
colorsys
.
hsv_to_rgb
(
h
,
s
,
random_state
.
uniform
(
0.2
,
1
))
return
'rgb({:.0f}, {:.0f}, {:.0f})'
.
format
(
r
*
rgb_max
,
g
*
rgb_max
,
b
*
rgb_max
)
return
single_color_func
class
WordCloud
(
object
):
"""Word cloud object for generating and drawing.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment