Skip to content

Commit

Permalink
Merge pull request #22 from facelessuser/feature/srgb-linear
Browse files Browse the repository at this point in the history
Expose access to srgb-linear
  • Loading branch information
facelessuser authored Mar 5, 2021
2 parents 3ab431c + 233fe8a commit e717109
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
3 changes: 2 additions & 1 deletion coloraide/colors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Colors."""
from .hsv import HSV
from .srgb import SRGB
from .srgb_linear import SRGB_Linear
from .hsl import HSL
from .hwb import HWB
from .lab import LAB
Expand All @@ -18,7 +19,7 @@
DEF_DELTA_E = "76"

SUPPORTED = (
HSL, HWB, LAB, LCH, SRGB, HSV,
HSL, HWB, LAB, LCH, SRGB, SRGB_Linear, HSV,
Display_P3, A98_RGB, ProPhoto_RGB, Rec2020, XYZ
)

Expand Down
44 changes: 44 additions & 0 deletions coloraide/colors/srgb_linear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""SRGB Linear color class."""
from ._space import RE_DEFAULT_MATCH
from .srgb import SRGB, lin_srgb_to_xyz, xyz_to_lin_srgb, lin_srgb, gam_srgb
from .xyz import XYZ
from . import _convert as convert
import re


class SRGB_Linear(SRGB):
"""SRGB linear."""

SPACE = "srgb-linear"
DEF_VALUE = "color(srgb-linear 0 0 0 / 1)"
DEFAULT_MATCH = re.compile(RE_DEFAULT_MATCH.format(color_space=SPACE))
WHITE = convert.WHITES["D65"]

def __init__(self, color=DEF_VALUE):
"""Initialize."""

super().__init__(color)

@classmethod
def _to_srgb(cls, rgb):
"""Linear sRGB to sRGB."""

return gam_srgb(rgb)

@classmethod
def _from_srgb(cls, rgb):
"""sRGB to linear sRGB."""

return lin_srgb(rgb)

@classmethod
def _to_xyz(cls, rgb):
"""SRGB Linear to XYZ."""

return cls._chromatic_adaption(cls.white(), XYZ.white(), lin_srgb_to_xyz(rgb))

@classmethod
def _from_xyz(cls, xyz):
"""XYZ to SRGB Linear."""

return xyz_to_lin_srgb(cls._chromatic_adaption(XYZ.white(), cls.white(), xyz))
3 changes: 2 additions & 1 deletion coloraide/css/colors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .hwb import HWB
from .lab import LAB
from .lch import LCH
from ...colors import SRGB_Linear
from ...colors import HSV
from ...colors import Display_P3
from ...colors import A98_RGB
Expand All @@ -13,7 +14,7 @@
from ...colors import Color as GenericColor

SUPPORTED = (
HSL, HWB, LAB, LCH, SRGB, HSV,
HSL, HWB, LAB, LCH, SRGB, SRGB_Linear, HSV,
Display_P3, A98_RGB, ProPhoto_RGB, Rec2020, XYZ
)

Expand Down
2 changes: 2 additions & 0 deletions docs/src/markdown/about/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.1.0a2

- **NEW**: Expose access to `srgb-linear` color space. This is mainly for development and testing and not listed in docs
currently.
- **FIX**: Cylindrical spaces, when calling `overlay` can now request to be overlaid in a different space. This is
because alpha composition does not work well in cylindrical spaces. HSL, HSV, and HWB will now request `overlay` to be
done in sRGB, and LCH will request overlay to be done in LAB.
Expand Down
7 changes: 6 additions & 1 deletion tests/test_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ def assert_round_trip(self, color):
def test_srgb(self):
"""Test sRGB."""

self.assert_round_trip("rgb(10% 200% 50%)")
self.assert_round_trip("rgb(10% 100% 50%)")

def test_srgb_linear(self):
"""Test sRGB Linear."""

self.assert_round_trip("color(srgb-linear 10% 100% 50%)")

def test_hsl(self):
"""Test HSL."""
Expand Down

0 comments on commit e717109

Please sign in to comment.