Skip to content

Commit

Permalink
convert tag to upper case in from_tag(), gamut-map in sRGB before con…
Browse files Browse the repository at this point in the history
…verting to RGB256
  • Loading branch information
apparebit committed May 25, 2024
1 parent 143d25d commit 1a3984e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions prettypretty/fidelity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import enum
from typing import cast, Literal, Self, TypeAlias

from .color.conversion import get_converter
from .color.conversion import get_converter, srgb_to_rgb256
from .color.gamut import map_into_gamut
from .color.lores import rgb6_to_eight_bit
from .color.spec import ColorSpec

Expand Down Expand Up @@ -65,7 +66,10 @@ def from_tag(cls, tag: FidelityTag | Self) -> Self:
return tag

assert isinstance(tag, str)
return cls[tag]
try:
return cls[tag]
except KeyError:
return cls[tag.upper()]

@classmethod
def from_color(cls, color: None | ColorSpec) -> 'None | Fidelity':
Expand Down Expand Up @@ -160,6 +164,12 @@ def prepare_to_render(self, color: None | ColorSpec) -> None | ColorSpec:
# Do not relabel for other fidelity levels, SGR parameters differ.
return ColorSpec('ansi', color.coordinates)

if self is Fidelity.RGB256:
# Ensure color is in sRGB gamut before converting into RGB256
coordinates = get_converter(color.tag, 'srgb')(*color.coordinates)
coordinates = map_into_gamut('srgb', coordinates)
return ColorSpec('rgb256', srgb_to_rgb256(*coordinates))

return ColorSpec(
self.tag,
get_converter(color.tag, self.tag)(*color.coordinates)
Expand Down

0 comments on commit 1a3984e

Please sign in to comment.