Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colour scheme is now consistent within MSlice #756

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions mslice/models/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from __future__ import (absolute_import, division)

from matplotlib import rcParams

from six import iteritems
try:
from matplotlib.colors import to_hex
Expand All @@ -35,9 +36,9 @@ def to_hex(color):
def mpl_named_colors():
return cnames

_BASIC_COLORS_PRETTY_NAME = {'b': 'blue', 'g': 'green', 'r': 'red', 'c': 'cyan', 'm': 'magenta', 'y': 'yellow',
'k': 'black', 'w': 'white'}
_BASIC_COLORS_HEX_MAPPING = dict((k, to_hex(k)) for k, _ in iteritems(_BASIC_COLORS_PRETTY_NAME))
_BASIC_COLORS_HEX_MAPPING = {'blue': '#1f77b4', 'orange': '#ff7f0e', 'green': '#2ca02c', 'red': '#d62728',
'purple': '#9467bd', 'brown': '#8c564b', 'pink': '#e377c2', 'gray': '#7f7f7f',
'olive': '#bcbd22', 'cyan': '#17becf'}


def pretty_name(name):
Expand Down Expand Up @@ -71,17 +72,17 @@ def named_cycle_colors():

def name_to_color(name):
"""
Translate between a our string names and the mpl color
Translate between our string names and the mpl color
representation
:param name: One of our known string names
:return: The string identifier we have chosen
:raises: ValueError if the color is not known
"""
try:
return mpl_named_colors()[name]
return _BASIC_COLORS_HEX_MAPPING[name]
except KeyError:
try:
return _BASIC_COLORS_HEX_MAPPING[name]
return mpl_named_colors()[name]
except KeyError:
raise ValueError("Color name {} unknown".format(name))

Expand All @@ -95,12 +96,12 @@ def color_to_name(color):
:raises: ValueError if the color is not known
"""
color_as_hex = to_hex(color)
for name, value in iteritems(mpl_named_colors()):
if color_as_hex == to_hex(value):
return pretty_name(name)
for name, hexvalue in iteritems(_BASIC_COLORS_HEX_MAPPING):
if color_as_hex == hexvalue:
return name
else:
for name, hexvalue in iteritems(_BASIC_COLORS_HEX_MAPPING):
if color_as_hex == hexvalue:
return name
for name, value in iteritems(mpl_named_colors()):
if color_as_hex == to_hex(value):
return pretty_name(name)
else:
raise ValueError("matplotlib color {} unknown".format(color))
6 changes: 3 additions & 3 deletions mslice/plotting/plot_window/cut_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import warnings
import numpy as np

from mslice.models.colors import to_hex
from mslice.models.colors import to_hex, name_to_color
from mslice.presenters.plot_options_presenter import CutPlotOptionsPresenter
from mslice.presenters.quick_options_presenter import quick_options, check_latex
from mslice.plotting.plot_window.plot_options import CutPlotOptions
Expand Down Expand Up @@ -208,7 +208,7 @@ def set_line_options(self, line, line_options):
line.set_label(line_options['label'])
line.set_linestyle(line_options['style'])
line.set_marker(line_options['marker'])
line.set_color(line_options['color'])
line.set_color(name_to_color(line_options['color']))
line.set_linewidth(line_options['width'])

def get_all_line_options(self):
Expand Down Expand Up @@ -267,7 +267,7 @@ def set_line_options_by_index(self, line_index, line_options):
self.toggle_errorbar(line_index, line_options)

for child in container.get_children():
child.set_color(line_options['color'])
child.set_color(name_to_color(line_options['color']))
child.set_linewidth(line_options['width'])
child.set_visible(line_options['shown'])

Expand Down
4 changes: 2 additions & 2 deletions mslice/plotting/plot_window/slice_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from matplotlib.legend import Legend
from matplotlib.text import Text

from mslice.models.colors import to_hex
from mslice.models.colors import to_hex, name_to_color
from mslice.models.units import get_sample_temperature_from_string
from mslice.presenters.plot_options_presenter import SlicePlotOptionsPresenter
from mslice.presenters.quick_options_presenter import quick_options, check_latex
Expand Down Expand Up @@ -233,7 +233,7 @@ def set_line_options(self, line, line_options):
line.set_label(line_options['label'])
line.set_linestyle(line_options['style'])
line.set_marker(line_options['marker'])
line.set_color(line_options['color'])
line.set_color(name_to_color(line_options['color']))
line.set_linewidth(line_options['width'])

def calc_figure_boundaries(self):
Expand Down
6 changes: 3 additions & 3 deletions mslice/tests/colors_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def test_color_names_do_not_contain_prefixes(self):
self.assertTrue(':' not in name)

def test_known_color_name_gives_expected_hex(self):
self.assertEqual("#008000", name_to_color("green"))
self.assertEqual("#2ca02c", name_to_color("green"))

def test_known_hex_gives_expected_color_name(self):
self.assertEqual("green", color_to_name("#008000"))
self.assertEqual("green", color_to_name("#2ca02c"))

def test_unknown_color_name_raises_valueerror(self):
self.assertRaises(ValueError, name_to_color, "NotAColorName")
Expand All @@ -26,7 +26,7 @@ def test_unknown_hex_color_raises_valueerror(self):
self.assertRaises(ValueError, color_to_name, "#12345")

def test_basic_color_is_known(self):
self.assertEqual('c', color_to_name('#00bfbf'))
self.assertEqual('cyan', color_to_name('#17becf'))


if __name__ == '__main__':
Expand Down
10 changes: 5 additions & 5 deletions mslice/tests/quick_options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def setup_line_values(qlo_mock):
type(quick_line_options).width = PropertyMock(return_value='5')
type(quick_line_options).label = PropertyMock(return_value='label2')
type(quick_line_options).shown = PropertyMock(return_value=True)
target = Line2D([], [], 3, '-', 'red', 'o', label='label1')
target = Line2D([], [], 3, '-', '#d62728', 'o', label='label1')
return qlo_mock, target


Expand Down Expand Up @@ -68,11 +68,11 @@ def test_line_slice(self, qlo_mock, show_legends):
quick_options(target, model)
# check view is called with existing line parameters
qlo_mock.assert_called_with(
{'shown': None, 'color': '#ff0000', 'label': u'label1', 'style': '-', 'width': '3',
{'shown': None, 'color': '#d62728', 'label': u'label1', 'style': '-', 'width': '3',
'marker': 'o', 'legend': None, 'error_bar': None}, True)
# check model is updated with parameters from view
self.assertDictEqual(model.get_line_options(target),
{'shown': None, 'color': '#0000ff', 'label': u'label2',
{'shown': None, 'color': '#1f77b4', 'label': u'label2',
'style': '--', 'width': '5', 'marker': '.', 'legend': None,
'error_bar': None})

Expand All @@ -97,11 +97,11 @@ def test_line_cut(self, qlo_mock, show_legends):
quick_options(target, model)
# check view is called with existing line parameters
qlo_mock.assert_called_with(
{'shown': True, 'color': '#ff0000', 'label': u'label1', 'style': '-', 'width': '3',
{'shown': True, 'color': '#d62728', 'label': u'label1', 'style': '-', 'width': '3',
'marker': 'o', 'legend': True, 'error_bar': False}, True)
# check model is updated with parameters from view
self.assertDictEqual(model.get_line_options(target),
{'shown': True, 'color': '#0000ff', 'label': u'label2',
{'shown': True, 'color': '#1f77b4', 'label': u'label2',
'style': '--', 'width': '5', 'marker': '.', 'legend': True, 'error_bar': False})

@patch.object(QuickAxisOptions, '__init__', lambda t, u, v, w, x, y, z: None)
Expand Down