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

Fix bug in custom color maps related to colors defined in RGB #1059

Merged
merged 1 commit into from
Aug 13, 2024
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
6 changes: 3 additions & 3 deletions examples/serve/demo/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,17 @@ CustomColorMaps:
- Identifier: cmap_bloom_risk
Type: categorical
Colors:
- [ 0, [0, 1, 0., 0.5]]
- [ 0, [0, 255, 0., 0.5]]
- [ 1, orange]
- [ 2, [1, 0, 0]]
- Identifier: s2_l2_scl
Type: categorical
Colors:
- [ 0, red, no data]
- [ 0, [255, 0, 0], no data]
- [ 1, yellow, defective]
- [ 2, black, dark area pixels]
- [ 3, gray, cloud shadows]
- [ 4, green, vegetation]
- [ 4, [0, 1, 0], vegetation]
- [ 5, tan, bare soils]
- [ 6, blue, water]
- [ 7, "#aaaabb", clouds low prob ]
Expand Down
5 changes: 2 additions & 3 deletions test/util/test_cmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,14 @@ def test_create_colormap_from_config_color_entry_object(self):
self.assertCountEqual([0, 12, 24], cmap.values)
colors = cmap.cmap(np.array([0, 0.5, 1]))
self.assertEqual([1.0, 0.0, 0.0, 1.0], list(colors[0]))
self.assertEqual([0.0, 1.0, 0.0, 0.3], list(colors[2]))
self.assertEqual(
{
"name": "my_cmap",
"type": "continuous",
"colors": [
[0.0, "red", "low"],
[12.0, "#0000FF", "medium"],
[24.0, [0, 1, 0, 0.3], "high"],
[24.0, "#00ff004c", "high"],
],
},
config_parse,
Expand Down Expand Up @@ -437,7 +436,7 @@ def test_create_colormap_from_config_color_entry_tuple(self):
"colors": [
[0.0, "red", "low"],
[1.0, "#0000FF"],
[2.0, [0, 1, 0], "high"],
[2.0, "#00ff00ff", "high"],
],
},
config_parse,
Expand Down
11 changes: 6 additions & 5 deletions xcube/util/cmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,18 +638,19 @@ def create_colormap_from_config(cmap_config: dict) -> Colormap:
colors = []
for color in cmap_config["Colors"]:
if isinstance(color, list):
colors.append(color)
colors.append([c for c in color])
else:
if "Label" in color:
colors.append([color["Value"], color["Color"], color["Label"]])
else:
colors.append([color["Value"], color["Color"]])

for i, item in enumerate(colors):
color = item[1]
if isinstance(color, list):
if any([val > 1 for val in color]):
colors[i][1] = list(np.array(color) / 255)
if isinstance(item[1], list):
color = [c for c in item[1]]
if any([val > 1 for val in color[:3]]):
color[:3] = np.array(color[:3]) / 255
colors[i][1] = matplotlib.colors.rgb2hex(color, keep_alpha=True)
config_parse = dict(
name=cmap_config["Identifier"],
type=cmap_config["Type"],
Expand Down
Loading