Skip to content

Instantly share code, notes, and snippets.

@facelessuser
Last active March 6, 2023 05:58
Show Gist options
  • Save facelessuser/afd14b9425b996c6f91b30a8312488bc to your computer and use it in GitHub Desktop.
Save facelessuser/afd14b9425b996c6f91b30a8312488bc to your computer and use it in GitHub Desktop.
LMS Plugin using Smith & Pokorny (1975) 2-deg cone fundamentals
# pragma: init
"""Convert Linear sRGB to LMS using Smith & Pokorny (1975) 2-deg cone fundamentals."""
from __future__ import annotations
from coloraide.cat import WHITES
from coloraide.spaces import Space
from coloraide import algebra as alg
from coloraide.types import Vector
from coloraide.channels import Channel
LRGB_TO_LMS = [
[0.178824041258, 0.4351609057000001, 0.04119349692],
[0.034556423182, 0.27155382458, 0.038671308360000003],
[0.000299565576, 0.0018430896, 0.01467086136]
]
LMS_TO_LRGB = [
[8.094435598032371, -13.050431460496926, 11.672058453917323],
[-1.0248505586646686, 5.401931309674973, -11.361471490598712],
[-0.03652974715933318, -0.412162807001268, 69.35132423820858]
]
class LMS(Space):
"""The LMS class."""
BASE = "srgb-linear"
NAME = "lms"
SERIALIZE = ("--lms",)
CHANNELS = (
Channel("l", 0.0, 1.0),
Channel("m", 0.0, 1.0),
Channel("s", 0.0, 1.0)
)
CHANNEL_ALIASES = {
"long": "l",
"medium": "m",
"short": "s"
}
WHITE = WHITES['2deg']['D65']
def to_base(self, coords: Vector) -> Vector:
"""To XYZ."""
return alg.dot(LMS_TO_LRGB, coords, dims=alg.D2_D1)
def from_base(self, coords: Vector) -> Vector:
"""From XYZ."""
return alg.dot(LRGB_TO_LMS, coords, dims=alg.D2_D1)
from coloraide import Color as Base
class Color(Base):
...
Color.register(LMS())
# pragma: init
Color('red').convert('lms')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment