Skip to content

Commit 6f8e1ad

Browse files
authored
Update LMS matrices (#244)
1 parent f223e67 commit 6f8e1ad

File tree

2 files changed

+64
-54
lines changed

2 files changed

+64
-54
lines changed

lib/sass/value/color/conversions.rb

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,35 @@ class Color
77
# @see https://github.com/sass/dart-sass/blob/main/lib/src/value/color/conversions.dart
88
module Conversions
99
# The D50 white point.
10+
#
11+
# Definition from https://www.w3.org/TR/css-color-4/#color-conversion-code.
1012
D50 = [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585].freeze
1113

14+
# Matrix values from https://www.w3.org/TR/css-color-4/#color-conversion-code.
15+
1216
# The transformation matrix for converting LMS colors to OKLab.
1317
#
14-
# Note that this can't be directly multiplied with {XYZ_D65_TO_LMS}; see Color
18+
# Note that this can't be directly multiplied with [XYZ_D65_TO_LMS]; see Color
1519
# Level 4 spec for details on how to convert between XYZ and OKLab.
1620
LMS_TO_OKLAB = [
17-
0.2104542553, 0.7936177850, -0.0040720468,
18-
1.9779984951, -2.4285922050, 0.4505937099,
19-
0.0259040371, 0.7827717662, -0.8086757660
21+
0.21045426830931400, 0.79361777470230540, -0.00407204301161930,
22+
1.97799853243116840, -2.42859224204858000, 0.45059370961741100,
23+
0.02590404246554780, 0.78277171245752960, -0.80867575492307740
2024
].freeze
2125

2226
# The transformation matrix for converting OKLab colors to LMS.
2327
#
24-
# Note that this can't be directly multiplied with {LMS_TO_XYZ_D65}; see Color
28+
# Note that this can't be directly multiplied with [LMS_TO_XYZ_D65]; see Color
2529
# Level 4 spec for details on how to convert between XYZ and OKLab.
2630
OKLAB_TO_LMS = [
27-
0.99999999845051981432, 0.396337792173767856780, 0.215803758060758803390,
28-
1.00000000888176077670, -0.105561342323656349400, -0.063854174771705903402,
29-
1.00000005467241091770, -0.089484182094965759684, -1.291485537864091739900
31+
1.00000000000000020, 0.39633777737617490, 0.21580375730991360,
32+
0.99999999999999980, -0.10556134581565854, -0.06385417282581334,
33+
0.99999999999999990, -0.08948417752981180, -1.29148554801940940
3034
].freeze
3135

36+
# The following matrices were precomputed using
37+
# https://gist.github.com/nex3/3d7ecfef467b22e02e7a666db1b8a316.
38+
3239
# The transformation matrix for converting linear-light srgb colors to
3340
# linear-light display-p3.
3441
LINEAR_SRGB_TO_LINEAR_DISPLAY_P3 = [
@@ -38,7 +45,7 @@ module Conversions
3845
].freeze
3946

4047
# The transformation matrix for converting linear-light display-p3 colors to
41-
# linear-light srgb.
48+
# linear-light srgb.
4249
LINEAR_DISPLAY_P3_TO_LINEAR_SRGB = [
4350
1.22494017628055980, -0.22494017628055996, 0.00000000000000000,
4451
-0.04205695470968816, 1.04205695470968800, 0.00000000000000000,
@@ -93,16 +100,16 @@ module Conversions
93100

94101
# The transformation matrix for converting linear-light srgb colors to lms.
95102
LINEAR_SRGB_TO_LMS = [
96-
0.41222147080000016, 0.53633253629999990, 0.05144599290000001,
97-
0.21190349820000007, 0.68069954509999990, 0.10739695660000000,
98-
0.08830246190000005, 0.28171883759999994, 0.62997870050000000
103+
0.41222146947076300, 0.53633253726173480, 0.05144599326750220,
104+
0.21190349581782520, 0.68069955064523420, 0.10739695353694050,
105+
0.08830245919005641, 0.28171883913612150, 0.62997870167382210
99106
].freeze
100107

101108
# The transformation matrix for converting lms colors to linear-light srgb.
102109
LMS_TO_LINEAR_SRGB = [
103-
4.07674166134799300, -3.30771159040819240, 0.23096992872942781,
104-
-1.26843800409217660, 2.60975740066337240, -0.34131939631021974,
105-
-0.00419608654183720, -0.70341861445944950, 1.70761470093094480
110+
4.07674163607595800, -3.30771153925806200, 0.23096990318210417,
111+
-1.26843797328503200, 2.60975734928768900, -0.34131937600265710,
112+
-0.00419607613867551, -0.70341861793593630, 1.70761469407461200
106113
].freeze
107114

108115
# The transformation matrix for converting linear-light srgb colors to
@@ -121,14 +128,16 @@ module Conversions
121128
-0.00855882878391742, -0.15326670213803720, 1.16182553092195470
122129
].freeze
123130

124-
# The transformation matrix for converting linear-light srgb colors to xyz-d50.
131+
# The transformation matrix for converting linear-light srgb colors to
132+
# xyz-d50.
125133
LINEAR_SRGB_TO_XYZ_D50 = [
126134
0.43606574687426936, 0.38515150959015960, 0.14307841996513868,
127135
0.22249317711056518, 0.71688701309448240, 0.06061980979495235,
128136
0.01392392146316939, 0.09708132423141015, 0.71409935681588070
129137
].freeze
130138

131-
# The transformation matrix for converting xyz-d50 colors to linear-light srgb.
139+
# The transformation matrix for converting xyz-d50 colors to linear-light
140+
# srgb.
132141
XYZ_D50_TO_LINEAR_SRGB = [
133142
3.13413585290011780, -1.61738599801804200, -0.49066221791109754,
134143
-0.97879547655577770, 1.91625437739598840, 0.03344287339036693,
@@ -186,17 +195,17 @@ module Conversions
186195
# The transformation matrix for converting linear-light display-p3 colors to
187196
# lms.
188197
LINEAR_DISPLAY_P3_TO_LMS = [
189-
0.48137985442585490, 0.46211836973903553, 0.05650177583510960,
190-
0.22883194490233110, 0.65321681282840370, 0.11795124216926511,
191-
0.08394575573016760, 0.22416526885956980, 0.69188897541026260
198+
0.48137985274995443, 0.46211837101131803, 0.05650177623872756,
199+
0.22883194181124472, 0.65321681938356760, 0.11795123880518774,
200+
0.08394575232299319, 0.22416527097756642, 0.69188897669944040
192201
].freeze
193202

194203
# The transformation matrix for converting lms colors to linear-light
195204
# display-p3.
196205
LMS_TO_LINEAR_DISPLAY_P3 = [
197-
3.12776898667772140, -2.25713579553953770, 0.12936680863610234,
198-
-1.09100904738343900, 2.41333175827934370, -0.32232271065457110,
199-
-0.02601081320950207, -0.50804132569306730, 1.53405213885176520
206+
3.12776897136187370, -2.25713576259163860, 0.12936679122976494,
207+
-1.09100901843779790, 2.41333171030692250, -0.32232269186912466,
208+
-0.02601080193857045, -0.50804133170416700, 1.53405213364273730
200209
].freeze
201210

202211
# The transformation matrix for converting linear-light display-p3 colors to
@@ -263,16 +272,16 @@ module Conversions
263272

264273
# The transformation matrix for converting linear-light a98-rgb colors to lms.
265274
LINEAR_A98_RGB_TO_LMS = [
266-
0.57643226147714040, 0.36991322114441194, 0.05365451737844765,
267-
0.29631647387335260, 0.59167612662650690, 0.11200739940014041,
268-
0.12347825480374285, 0.21949869580674647, 0.65702304938951070
275+
0.57643225961839410, 0.36991322261987963, 0.05365451776172635,
276+
0.29631647054222465, 0.59167613325218850, 0.11200739620558686,
277+
0.12347825101427760, 0.21949869837199862, 0.65702305061372380
269278
].freeze
270279

271280
# The transformation matrix for converting lms colors to linear-light a98-rgb.
272281
LMS_TO_LINEAR_A98_RGB = [
273-
2.55403684790806950, -1.62197620262602140, 0.06793935455575403,
274-
-1.26843800409217660, 2.60975740066337240, -0.34131939631021974,
275-
-0.05623474718052319, -0.56704183411879500, 1.62327658124261400
282+
2.55403683861155660, -1.62197618068286990, 0.06793934207131327,
283+
-1.26843797328503200, 2.60975734928768900, -0.34131937600265710,
284+
-0.05623473593749381, -0.56704183956690610, 1.62327657550439990
276285
].freeze
277286

278287
# The transformation matrix for converting linear-light a98-rgb colors to
@@ -323,16 +332,16 @@ module Conversions
323332

324333
# The transformation matrix for converting linear-light rec2020 colors to lms.
325334
LINEAR_REC2020_TO_LMS = [
326-
0.61675578719908560, 0.36019839939276255, 0.02304581340815186,
327-
0.26513306398328140, 0.63583936407771060, 0.09902757183900800,
328-
0.10010263423281572, 0.20390651940192997, 0.69599084636525430
335+
0.61675578486544440, 0.36019840122646335, 0.02304581390809228,
336+
0.26513305939263670, 0.63583937206784910, 0.09902756853951408,
337+
0.10010262952034828, 0.20390652261661452, 0.69599084786303720
329338
].freeze
330339

331340
# The transformation matrix for converting lms colors to linear-light rec2020.
332341
LMS_TO_LINEAR_REC2020 = [
333-
2.13990673569556170, -1.24638950878469060, 0.10648277296448995,
334-
-0.88473586245815630, 2.16323098210838260, -0.27849511943390290,
335-
-0.04857375801465988, -0.45450314291725170, 1.50307690088646130
342+
2.13990673043465130, -1.24638949376061800, 0.10648276332596668,
343+
-0.88473583575776740, 2.16323093836120070, -0.27849510260343340,
344+
-0.04857374640044396, -0.45450314971409640, 1.50307689611454040
336345
].freeze
337346

338347
# The transformation matrix for converting linear-light rec2020 colors to
@@ -369,16 +378,16 @@ module Conversions
369378

370379
# The transformation matrix for converting xyz colors to lms.
371380
XYZ_D65_TO_LMS = [
372-
0.81902244321643190, 0.36190625628012210, -0.12887378261216414,
373-
0.03298366719802710, 0.92928684689655460, 0.03614466816999844,
374-
0.04817719956604625, 0.26423952494422764, 0.63354782581369370
381+
0.81902243799670300, 0.36190626005289034, -0.12887378152098788,
382+
0.03298365393238846, 0.92928686158634330, 0.03614466635064235,
383+
0.04817718935962420, 0.26423953175273080, 0.63354782846943080
375384
].freeze
376385

377386
# The transformation matrix for converting lms colors to xyz.
378387
LMS_TO_XYZ_D65 = [
379-
1.22687987337415570, -0.55781499655548140, 0.28139105017721590,
380-
-0.04057576262431372, 1.11228682939705960, -0.07171106666151703,
381-
-0.07637294974672143, -0.42149332396279143, 1.58692402442724180
388+
1.22687987584592430, -0.55781499446021710, 0.28139104566596460,
389+
-0.04057574521480084, 1.11228680328031730, -0.07171105806551635,
390+
-0.07637293667466007, -0.42149333240224324, 1.58692401983678180
382391
].freeze
383392

384393
# The transformation matrix for converting xyz colors to linear-light
@@ -414,31 +423,31 @@ module Conversions
414423
# The transformation matrix for converting lms colors to linear-light
415424
# prophoto-rgb.
416425
LMS_TO_LINEAR_PROPHOTO_RGB = [
417-
1.73835514985815240, -0.98795095237343430, 0.24959580241648663,
418-
-0.70704942624914860, 1.93437008438177620, -0.22732065793919040,
419-
-0.08407883426424761, -0.35754059702097796, 1.44161943124947150
426+
1.73835514811572070, -0.98795094275144580, 0.24959579463572504,
427+
-0.70704940153292660, 1.93437004444013820, -0.22732064290721150,
428+
-0.08407882206239634, -0.35754060521141334, 1.44161942727380970
420429
].freeze
421430

422431
# The transformation matrix for converting linear-light prophoto-rgb colors to
423432
# lms.
424433
LINEAR_PROPHOTO_RGB_TO_LMS = [
425-
0.71544846349294310, 0.35279154798172740, -0.06824001147467047,
426-
0.27441165509049420, 0.66779764080811480, 0.05779070400139092,
427-
0.10978443849083751, 0.18619828746596980, 0.70401727404319270
434+
0.71544846056555340, 0.35279155007721186, -0.06824001064276530,
435+
0.27441164900156710, 0.66779764984123670, 0.05779070115719616,
436+
0.10978443261622942, 0.18619829115002018, 0.70401727623375040
428437
].freeze
429438

430439
# The transformation matrix for converting lms colors to xyz-d50.
431440
LMS_TO_XYZ_D50 = [
432-
1.28858621583908840, -0.53787174651736210, 0.21358120705405403,
433-
-0.00253389352489796, 1.09231682453266550, -0.08978293089853581,
434-
-0.06937383312514489, -0.29500839218634667, 1.18948682779245090
441+
1.28858621817270600, -0.53787174449737450, 0.21358120275423640,
442+
-0.00253387643187372, 1.09231679887191650, -0.08978292244004273,
443+
-0.06937382305734124, -0.29500839894431263, 1.18948682451211420
435444
].freeze
436445

437446
# The transformation matrix for converting xyz-d50 colors to lms.
438447
XYZ_D50_TO_LMS = [
439-
0.77070004712402500, 0.34924839871072740, -0.11202352004249890,
440-
0.00559650559780223, 0.93707232493333150, 0.06972569131301698,
441-
0.04633715253432816, 0.25277530868525870, 0.85145807371608350
448+
0.77070004204311720, 0.34924840261939616, -0.11202351884164681,
449+
0.00559649248368848, 0.93707234011367690, 0.06972568836252771,
450+
0.04633714262191069, 0.25277531574310524, 0.85145807674679600
442451
].freeze
443452

444453
# The transformation matrix for converting linear-light prophoto-rgb colors to

spec/spec_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
supports_block_expectations
4545
end
4646

47-
precision = Sass::Value.const_get(:FuzzyMath)::PRECISION
47+
# TODO: test with higher precision
48+
precision = Sass::Value.const_get(:FuzzyMath)::PRECISION - 6
4849

4950
RSpec::Matchers.matcher :fuzzy_eq do |expected|
5051
match do |actual|

0 commit comments

Comments
 (0)