Skip to content

Commit

Permalink
addressed Normans Review
Browse files Browse the repository at this point in the history
  • Loading branch information
konstntokas committed Aug 8, 2024
1 parent e864840 commit a3cfac6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
14 changes: 10 additions & 4 deletions test/util/test_cmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,11 @@ def test_create_colormap_from_config_color_entry_object(self):
{
"name": "my_cmap",
"type": "continuous",
"colors": [[0.0, "red"], [12.0, "#0000FF"], [24.0, [0, 1, 0, 0.3]]],
"labels": ["low", "medium", "high"],
"colors": [
[0.0, "red", "low"],
[12.0, "#0000FF", "medium"],
[24.0, [0, 1, 0, 0.3], "high"],
],
},
config_parse,
)
Expand Down Expand Up @@ -431,8 +434,11 @@ def test_create_colormap_from_config_color_entry_tuple(self):
{
"name": "my_cmap",
"type": "categorical",
"colors": [[0.0, "red"], [1.0, "#0000FF"], [2.0, [0, 1, 0]]],
"labels": ["low", "", "high"],
"colors": [
[0.0, "red", "low"],
[1.0, "#0000FF"],
[2.0, [0, 1, 0], "high"],
],
},
config_parse,
)
17 changes: 6 additions & 11 deletions xcube/util/cmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ def parse_cm_code(cm_code: str) -> tuple[str, Optional[Colormap]]:
user_color_map: dict[str, Any] = json.loads(cm_code)
cm_name = user_color_map["name"]
cm_items = user_color_map["colors"]
cm_items = [item[:2] for item in cm_items]
cm_type = user_color_map.get("type", "continuous")
cm_base_name, _, _ = parse_cm_name(cm_name)
n = len(cm_items)
Expand Down Expand Up @@ -635,30 +636,24 @@ def get_cmap_png_base64(cmap: matplotlib.colors.Colormap) -> str:
def create_colormap_from_config(cmap_config: dict) -> Colormap:
registry = ColormapRegistry()
colors = []
labels = []
for color in cmap_config["Colors"]:
if isinstance(color, list):
colors.append(color[:2])
if len(color) == 3:
labels.append(color[2])
else:
labels.append("")
colors.append(color)
else:
colors.append([color["Value"], color["Color"]])
if "Label" in color:
labels.append(color["Label"])
colors.append([color["Value"], color["Color"], color["Label"]])
else:
labels.append("")
colors.append([color["Value"], color["Color"]])

for i, [_, color] in enumerate(colors):
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)
config_parse = dict(
name=cmap_config["Identifier"],
type=cmap_config["Type"],
colors=colors,
labels=labels,
)
_, cmap = registry.get_cmap(json.dumps(config_parse))
return cmap, config_parse
Expand Down

0 comments on commit a3cfac6

Please sign in to comment.