Skip to content

Commit

Permalink
iss #381 modify _adjust_color_lightness function and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaMorandi committed May 8, 2023
1 parent 254d5c1 commit 5e06adb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
38 changes: 19 additions & 19 deletions brightwind/analyse/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,26 @@ def _colormap_to_colorscale(cmap, n_colors):
return [to_hex(cmap(k*1/(n_colors-1))) for k in range(n_colors)]


def _adjust_color_lightness(r, g, b, factor):
def _adjust_color_lightness(input_color, lightness_factor):
"""
Generate the color corresponding to the input primary color corrected by a lightness or darkness defined by the
input factor percentage. Lighter colors are obtained with a factor >1 and darker colors with a factor <1.
:param r: Intensity of red color between 0 and 255.
:type r: float
:param g: Intensity of green color between 0 and 255.
:type g: float
:param b: Intensity of blue color between 0 and 255.
:type b: float
:param factor: Factor defining the percentage of lightness (>1) or darkness (<1).
:type factor: float
:return: color in hex format
:rtype: hex
Generate the color corresponding to the input primary color corrected by a lightness factor indicating the
percentage lightness. Lighter colors are obtained with a factor > 0.5 and darker colors with a factor < 0.5.
This function is converting the input color to hue, saturation, lightness (hsl) format and adjusting only the
lightness value. See https://www.w3schools.com/colors/colors_picker.asp for more info.
:param input_color: Input base color to adjust. It can accept any matplotlib recognised color inputs.
(see https://matplotlib.org/stable/tutorials/colors/colors.html) or `numpy.ma.masked`
:type input_color: str, tuple, list
:param lightness_factor: Percentage of lightness (>0.5) or darkness (<0.5). Value should be between 0 and 1.
:type lightness_factor: float
:return: color in hex format
:rtype: hex
"""
lightness_factor = utils.validate_coverage_threshold(lightness_factor)
r, g, b = tuple(255 * np.array(mpl.colors.to_rgb(input_color))) # convert to rgb format
hue, lightness, saturation = rgb2hls(r / 255.0, g / 255.0, b / 255.0)
lightness = max(min(lightness * factor, 1.0), 0.0)
r, g, b = hls2rgb(hue, lightness, saturation)
return rgb2hex(int(r * 255), int(g * 255), int(b * 255))
r, g, b = hls2rgb(hue, lightness_factor, saturation)
return mpl.colors.to_hex([r, g, b])


def plot_monthly_means(data, coverage=None, ylbl=''):
Expand Down Expand Up @@ -1222,7 +1222,7 @@ def _bar_subplot(data, x_label=None, y_label=None, min_bar_axis_limit=None, max_
for data_bar, data_bin in zip(data[name], data_bins):
if vertical_bars:
ax.imshow(np.array([[mpl.colors.to_rgb(bar_color)],
[mpl.colors.to_rgb(_adjust_color_lightness(r, g, b, factor=1.8))]]),
[mpl.colors.to_rgb(_adjust_color_lightness(bar_color, lightness_factor=0.8))]]),
interpolation='gaussian', extent=(data_bin + x_offset - bar_width / 2,
data_bin + x_offset + bar_width / 2, 0,
data_bar),
Expand All @@ -1231,7 +1231,7 @@ def _bar_subplot(data, x_label=None, y_label=None, min_bar_axis_limit=None, max_
edgecolor=bar_color, linewidth=line_width, fill=False,
zorder=1)#5
else:
cmp = _create_colormap(mpl.colors.to_rgb(_adjust_color_lightness(r, g, b, factor=1.8)),
cmp = _create_colormap(mpl.colors.to_rgb(_adjust_color_lightness(bar_color, lightness_factor=0.8)),
mpl.colors.to_rgb(bar_color))
ax.imshow(_gradient_image(direction=1, cmap_range=(0, 1)), cmap=cmp,
interpolation='gaussian',
Expand Down
12 changes: 12 additions & 0 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import numpy as np
from matplotlib.ticker import PercentFormatter
from matplotlib.dates import DateFormatter
import matplotlib as mpl
from colormap import rgb2hex, rgb2hls, hls2rgb

DATA = bw.load_csv(bw.demo_datasets.demo_data)
DATA = bw.apply_cleaning(DATA, bw.demo_datasets.demo_cleaning_file)
Expand Down Expand Up @@ -144,3 +146,13 @@ def test_plot_freq_distribution():
max_y_value=None, x_tick_labels=None, x_label=None,
y_label='count', total_width=1, legend=True)
assert True


def test_adjust_color_lightness():
input_color = '#9CC537'
r, g, b = tuple(255 * np.array(mpl.colors.to_rgb(input_color)))
hue, lightness, saturation = rgb2hls(r / 255, g / 255, b / 255)
r, g, b = tuple(255 * np.array(mpl.colors.to_rgb(bw.analyse.plot._adjust_color_lightness(input_color, 0.1))))
hue1, lightness1, saturation1 = rgb2hls(r / 255, g / 255, b / 255)
assert (int(hue * 100) == int(hue1 * 100)) and (int(saturation * 100) == int(saturation1 * 100)) and \
(lightness1 == 0.1)

0 comments on commit 5e06adb

Please sign in to comment.