Commit 39369d22 authored by Raphael Boidol's avatar Raphael Boidol
Browse files

add single color generating function

parent df20defd
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
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']
......@@ -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.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment