-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaces.go
More file actions
755 lines (696 loc) · 17.9 KB
/
spaces.go
File metadata and controls
755 lines (696 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
// SPDX-FileCopyrightText: 2024 Dominik Honnef and contributors
//
// SPDX-License-Identifier: MIT
package color
import (
"fmt"
"math"
"slices"
"strings"
"sync"
)
func init() {
RegisterSpace(XYZ_D50)
RegisterSpace(XYZ_D65)
RegisterSpace(LinearDisplayP3)
RegisterSpace(DisplayP3)
RegisterSpace(LinearSRGB)
RegisterSpace(SRGB)
RegisterSpace(Oklab)
RegisterSpace(Oklch)
RegisterSpace(ProPhoto)
RegisterSpace(LinearProPhoto)
RegisterSpace(Lab)
RegisterSpace(LCh)
RegisterSpace(LinearRec2020)
RegisterSpace(Rec2020)
RegisterSpace(Rec2100PQ)
}
var (
spacesMu sync.RWMutex
spaces = map[string]*Space{}
infty = [2]float64{math.Inf(-1), math.Inf(1)}
norm = [2]float64{0, 1}
)
// LookupSpace looks up a registered (see [RegisterSpace]) color space by ID.
func LookupSpace(id string) (*Space, bool) {
id = strings.TrimPrefix(id, "--")
spacesMu.RLock()
defer spacesMu.RUnlock()
cs, ok := spaces[id]
return cs, ok
}
// RegisterSpace registers a color space. This allows it to be referenced
// by ID in 'color()' expressions as parsed by [Parse] and looked up by
// [LookupSpace].
//
// All color spaces provided by this package are automatically registered.
func RegisterSpace(cs *Space) {
spacesMu.Lock()
defer spacesMu.Unlock()
registerSpace(cs)
}
func registerSpace(cs *Space) {
if _, ok := spaces[cs.ID]; ok {
// Trying to register the same color space ID more than once might point
// to a mistake, but it might also be the result of us registering base
// spaces, so we can't panic here.
return
}
spaces[cs.ID] = cs
if cs.Base != nil {
if _, ok := spaces[cs.Base.ID]; !ok {
registerSpace(cs.Base)
}
}
}
// Space describes a color space, such as sRGB or HSV.
//
// Color spaces form a tree. Every space, except for [XYZ_D65], has a base space
// and can be converted to and from it. Every space can be converted to any
// other space by finding a common "connection space". Often, the common space
// is XYZ D65.
//
// Color spaces are bit-width-agnostic and values are often stored in a
// normalized form. For example, the R, G, and B coordinates in sRGB will be in
// the range [0, 1], not [0, 255].
//
// The white point of a color space is described by the White field. This field
// only serves as documentation and does not affect how conversions are carried
// out. That is, simply changing the white point of an existing color space will
// not have the intended effect.
//
// When creating new color spaces, you must call [Space.Init] once you're
// done.
type Space struct {
ID string
Name string
White Chromaticity
Base *Space
Coords [3]Coordinate
FromBase func(c [3]float64) [3]float64
ToBase func(c [3]float64) [3]float64
path []*Space
}
func (cs *Space) Init() *Space {
if cs.Coords == ([3]Coordinate{}) {
cs.Coords = cs.Base.Coords
}
if cs.White == (Chromaticity{}) && cs.Base != nil {
cs.White = cs.Base.White
}
for i := range cs.Coords {
coord := &cs.Coords[i]
if coord.RefRange == ([2]float64{}) {
coord.RefRange = coord.Range
}
}
// if cs.GamutSpace == nil {
// var isPolar bool
// for _, coord := range cs.Coords {
// if coord.IsAngle {
// isPolar = true
// break
// }
// }
// if isPolar {
// cs.GamutSpace = cs.Base
// } else {
// cs.GamutSpace = cs
// }
// }
orig := cs
var out []*Space
for cs != nil {
out = append(out, cs)
cs = cs.Base
}
slices.Reverse(out)
orig.path = out
return orig
}
func (cs *Space) InGamut(values [3]float64) bool {
const ϵ = 0.000075
// if cs.GamutSpace != cs {
// values = cs.Convert(cs.GamutSpace, values)
// return cs.GamutSpace.InGamut(values)
// }
for i, v := range values {
meta := cs.Coords[i]
if !meta.IsAngle {
min := meta.Range[0]
max := meta.Range[1]
if !(v >= min-ϵ && v <= max+ϵ) {
return false
}
}
}
return true
}
func (cs *Space) Convert(to *Space, coords [3]float64) [3]float64 {
ourPath := cs.path
theirPath := to.path
// Determine the connection space by finding the lowest common ancestor of
// the source and destination spaces in the color space tree.
connIdx := -1
for i := range min(len(ourPath), len(theirPath)) {
if ourPath[i] == theirPath[i] {
connIdx = i
} else {
break
}
}
if connIdx == -1 {
// Every space should be connectable through XYZ.
panic(fmt.Sprintf("internal error: couldn't find connection space for %s and %s",
cs.Name, to.Name))
}
// Convert from our space to the connection space
for i := len(ourPath) - 1; i > connIdx; i-- {
coords = ourPath[i].ToBase(coords)
}
// Convert from connection space to destination space
for i := connIdx + 1; i < len(theirPath); i++ {
coords = theirPath[i].FromBase(coords)
}
return coords
}
// NewXYZSpace returns a new CIE XYZ color space with the specified name, ID, and
// white point.
func NewXYZSpace(name, id string, white Chromaticity) *Space {
// OPT(dh): because all white point conversions go through D65, converting
// between two non-D65 white points uses two instead of one matrix. For
// example, we'd do D50->D65->D75, instead of the more direct D50->D75. This
// is slower, and introduces more floating point error.
//
// In practice, most color spaces use D65 or D50, anyway.
toD65 := Bradford.Matrix(white, XYZ_D65.White)
fromD65 := Bradford.Matrix(XYZ_D65.White, white)
return (&Space{
ID: id,
Name: name,
White: white,
Base: XYZ_D65,
FromBase: func(c [3]float64) [3]float64 {
return Adapt(c, &fromD65)
},
ToBase: func(c [3]float64) [3]float64 {
return Adapt(c, &toD65)
},
}).Init()
}
var XYZ_D50 = NewXYZSpace("XYZ D50", "xyz-d50", WhitesCSSD50)
var XYZ_D65 = (&Space{
ID: "xyz-d65",
Name: "XYZ D65",
Coords: [3]Coordinate{
{Name: "X", Range: infty, RefRange: norm},
{Name: "Y", Range: infty, RefRange: norm},
{Name: "Z", Range: infty, RefRange: norm},
},
White: WhitesSRGBD65,
}).Init()
var LinearDisplayP3 = newRGBSpace(
&rgbSpace{
ID: "display-p3-linear",
Name: "Linear Display P3",
Base: XYZ_D65,
ToBase: [3][3]float64{
{0.4865709486482162, 0.26566769316909306, 0.1982172852343625},
{0.2289745640697488, 0.6917385218365064, 0.079286914093745},
{0.0000000000000000, 0.04511338185890264, 1.043944368900976},
},
FromBase: [3][3]float64{
{2.493496911941425, -0.9313836179191239, -0.40271078445071684},
{-0.8294889695615747, 1.7626640603183463, 0.023624685841943577},
{0.03584583024378447, -0.07617238926804182, 0.9568845240076872},
},
},
)
var DisplayP3 = (&Space{
ID: "display-p3",
Name: "Display P3",
Base: LinearDisplayP3,
// Gamma encoding is the same as sRGB
ToBase: SRGB.ToBase,
FromBase: SRGB.FromBase,
}).Init()
var LinearSRGB = newRGBSpace(
&rgbSpace{
ID: "srgb-linear",
Name: "Linear sRGB",
Base: XYZ_D65,
// sRGB Matrices taken from
// https://github.com/w3c/csswg-drafts/pull/7320/commits/e835926c83e10342c0c43fd2f1ccbff1b35c3f07
ToBase: [3][3]float64{
{506752.0 / 1228815.0, 87881.0 / 245763.0, 12673.0 / 70218.0},
{87098.0 / 409605.0, 175762.0 / 245763.0, 12673.0 / 175545.0},
{7918.0 / 409605.0, 87881.0 / 737289.0, 1001167.0 / 1053270.0},
},
FromBase: [3][3]float64{
{12831.0 / 3959.0, -329.0 / 214.0, -1974.0 / 3959.0},
{-851781.0 / 878810.0, 1648619.0 / 878810.0, 36519.0 / 878810.0},
{705.0 / 12673.0, -2585.0 / 12673.0, 705.0 / 667.0},
},
},
)
type rgbSpace struct {
ID string
Name string
Base *Space
ToBase [3][3]float64
FromBase [3][3]float64
}
func newRGBSpace(space *rgbSpace) *Space {
return (&Space{
ID: space.ID,
Name: space.Name,
Coords: RGBCoordinates,
Base: space.Base,
ToBase: func(c [3]float64) [3]float64 {
return mulVecMat(c, &space.ToBase)
},
FromBase: func(c [3]float64) [3]float64 {
return mulVecMat(c, &space.FromBase)
},
}).Init()
}
// SRGB is the sRGB color space. Conversion to sRGB uses the piece-wise OETF.
// Conversion from sRGB uses the inverse of the piece-wise OETF. This is not the
// same as the EOTF that is commonly used by consumer displays, which uses gamma
// 2.2.
var SRGB = (&Space{
ID: "srgb",
Name: "sRGB",
Base: LinearSRGB,
FromBase: func(c [3]float64) [3]float64 {
f := func(ch float64) float64 {
var sign float64
if ch < 0 {
sign = -1.0
} else {
sign = 1.0
}
abs := math.Abs(ch)
if abs > 0.0031308 {
return sign * (1.055*(math.Pow(abs, 1.0/2.4)) - 0.055)
} else {
return 12.92 * ch
}
}
return [3]float64{f(c[0]), f(c[1]), f(c[2])}
},
ToBase: func(c [3]float64) [3]float64 {
f := func(ch float64) float64 {
var sign float64
if ch < 0 {
sign = -1
} else {
sign = 1
}
abs := ch * sign
if abs <= 0.04045 {
return ch / 12.92
} else {
return sign * math.Pow((abs+0.055)/1.055, 2.4)
}
}
return [3]float64{f(c[0]), f(c[1]), f(c[2])}
},
}).Init()
// Matrices have been recalculated for consistent reference white;
// see https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-943521484
var (
oklabXyzToLms = [3][3]float64{
{0.8190224379967030, 0.3619062600528904, -0.1288737815209879},
{0.0329836539323885, 0.9292868615863434, 0.0361446663506424},
{0.0481771893596242, 0.2642395317527308, 0.6335478284694309},
}
oklabLmsToLab = [3][3]float64{
{0.2104542683093140, 0.7936177747023054, -0.0040720430116193},
{1.9779985324311684, -2.4285922420485799, 0.4505937096174110},
{0.0259040424655478, 0.7827717124575296, -0.8086757549230774},
}
oklabLabToLms = [3][3]float64{
{1.0000000000000000, 0.3963377773761749, 0.2158037573099136},
{1.0000000000000000, -0.1055613458156586, -0.0638541728258133},
{1.0000000000000000, -0.0894841775298119, -1.2914855480194092},
}
oklabLmsToXyz = [3][3]float64{
{1.2268798758459243, -0.5578149944602171, 0.2813910456659647},
{-0.0405757452148008, 1.1122868032803170, -0.0717110580655164},
{-0.0763729366746601, -0.4214933324022432, 1.5869240198367816},
}
)
var Oklab = (&Space{
ID: "oklab",
Name: "Oklab",
Coords: [3]Coordinate{
{Name: "Lightness", Range: infty, RefRange: norm},
{Name: "a", Range: infty, RefRange: [2]float64{-0.4, 0.4}},
{Name: "b", Range: infty, RefRange: [2]float64{-0.4, 0.4}},
},
Base: XYZ_D65,
FromBase: func(c [3]float64) [3]float64 {
lms := mulVecMat(c, &oklabXyzToLms)
lms_ := [3]float64{
math.Cbrt(lms[0]),
math.Cbrt(lms[1]),
math.Cbrt(lms[2]),
}
lab := mulVecMat(lms_, &oklabLmsToLab)
return lab
},
ToBase: func(c [3]float64) [3]float64 {
lms := mulVecMat(c, &oklabLabToLms)
lms_ := [3]float64{
lms[0] * lms[0] * lms[0],
lms[1] * lms[1] * lms[1],
lms[2] * lms[2] * lms[2],
}
xyz := mulVecMat(lms_, &oklabLmsToXyz)
return xyz
},
}).Init()
var Oklch = (&Space{
ID: "oklch",
Name: "Oklch",
Coords: [3]Coordinate{
{Name: "Lightness", Range: infty, RefRange: norm},
{Name: "Chroma", Range: infty, RefRange: [2]float64{0, 0.4}},
{Name: "Hue", Range: infty, IsAngle: true, RefRange: [2]float64{0, 360}},
},
Base: Oklab,
FromBase: func(c [3]float64) [3]float64 {
return labToLCH(c, 0.8/1e5)
},
ToBase: LCh.ToBase,
}).Init()
var Lab = (&Space{
ID: "lab",
Name: "Lab",
Coords: [3]Coordinate{
{Name: "Lightness", Range: infty, RefRange: [2]float64{0, 100}},
{Name: "a", Range: infty, RefRange: [2]float64{-125, 125}},
{Name: "b", Range: infty, RefRange: [2]float64{-125, 125}},
},
Base: XYZ_D50,
FromBase: func(c [3]float64) [3]float64 {
const (
ϵ = 216.0 / 24389.0
ϵ3 = 24.0 / 116.0
κ = 24389.0 / 27.0
)
white := WhitesCSSD50.XYZ()
xyz := c
xyz[0] /= white[0]
xyz[1] /= white[1]
xyz[2] /= white[2]
f := func(x float64) float64 {
if x > ϵ {
return math.Cbrt(x)
} else {
return (κ*x + 16) / 116.0
}
}
x_ := f(xyz[0])
y_ := f(xyz[1])
z_ := f(xyz[2])
l := 116.0*y_ - 16
a := 500.0 * (x_ - y_)
b := 200.0 * (y_ - z_)
return [3]float64{l, a, b}
},
ToBase: func(c [3]float64) [3]float64 {
const (
ϵ = 216.0 / 24389.0
ϵ3 = 24.0 / 116.0
κ = 24389.0 / 27.0
)
l, a, b := c[0], c[1], c[2]
f1 := (l + 16.0) / 116.0
f0 := a/500.0 + f1
f2 := f1 - b/200.0
var x, y, z float64
if f0 > ϵ3 {
x = f0 * f0 * f0
} else {
x = (116*f0 - 16) / κ
}
if l > 8 {
n := (l + 16.0) / 116.0
y = n * n * n
} else {
y = l / κ
}
if f2 > ϵ3 {
z = f2 * f2 * f2
} else {
z = (116.0*f2 - 16) / κ
}
white := WhitesCSSD50.XYZ()
x /= white[0]
y /= white[1]
z /= white[2]
return [3]float64{x, y, z}
},
}).Init()
var LCh = (&Space{
ID: "lch",
Name: "LCh",
Coords: [3]Coordinate{
{Name: "Lightness", Range: infty, RefRange: [2]float64{0, 100}},
{Name: "Chroma", Range: infty, RefRange: [2]float64{0, 150}},
{Name: "Hue", Range: infty, IsAngle: true, RefRange: [2]float64{0, 360}},
},
Base: Lab,
FromBase: func(c [3]float64) [3]float64 {
return labToLCH(c, 250.0/1e5)
},
ToBase: func(cl [3]float64) [3]float64 {
// XXX handle achromatic h
l, c, h := cl[0], cl[1], cl[2]
if c < 0 {
c = 0
}
a := c * math.Cos(h*math.Pi/180.0)
b := c * math.Sin(h*math.Pi/180)
return [3]float64{l, a, b}
},
}).Init()
func labToLCH(lab [3]float64, ϵ float64) [3]float64 {
l, a, b := lab[0], lab[1], lab[2]
achromatic := math.Abs(a) < ϵ && math.Abs(b) < ϵ
var c, h float64
if achromatic {
c = 0
// XXX color.js uses null for achromatic
h = 0
} else {
c = math.Sqrt(a*a + b*b)
h_ := math.Atan2(b, a) * 180 / math.Pi
h = math.Mod(h_+360, 360)
}
return [3]float64{l, c, h}
}
var LinearProPhoto = newRGBSpace(
&rgbSpace{
ID: "prophoto-rgb-linear",
Name: "Linear ProPhoto",
Base: XYZ_D50,
ToBase: [3][3]float64{
{0.79776664490064230, 0.13518129740053308, 0.03134773412839220},
{0.28807482881940130, 0.71183523424187300, 0.00008993693872564},
{0.00000000000000000, 0.00000000000000000, 0.82510460251046020},
},
FromBase: [3][3]float64{
{1.34578688164715830, -0.25557208737979464, -0.05110186497554526},
{-0.54463070512490190, 1.50824774284514680, 0.02052744743642139},
{0.00000000000000000, 0.00000000000000000, 1.21196754563894520},
},
},
)
var ProPhoto = (&Space{
ID: "prophoto-rgb",
Name: "ProPhoto",
Base: LinearProPhoto,
Coords: RGBCoordinates,
ToBase: func(c [3]float64) [3]float64 {
f := func(v float64) float64 {
var sign float64
if v < 0 {
sign = -1
} else {
sign = 1
}
abs := math.Abs(v)
if abs <= 16.0/512.0 {
return v / 16.0
} else {
return sign * math.Pow(abs, 1.8)
}
}
return [3]float64{
f(c[0]),
f(c[1]),
f(c[2]),
}
},
FromBase: func(c [3]float64) [3]float64 {
f := func(v float64) float64 {
var sign float64
if v < 0 {
sign = -1
} else {
sign = 1
}
abs := math.Abs(v)
if abs > 1.0/512.0 {
return sign * math.Pow(abs, 1.0/1.8)
} else {
return 16 * v
}
}
return [3]float64{
f(c[0]),
f(c[1]),
f(c[2]),
}
},
}).Init()
var LinearRec2020 = newRGBSpace(
&rgbSpace{
ID: "rec2020-linear",
Name: "Linear Rec. 2020",
Base: XYZ_D65,
ToBase: [3][3]float64{
{6.36953507e-01, 1.44619185e-01, 1.68855854e-01},
{2.62698339e-01, 6.78008766e-01, 5.92928953e-02},
{4.99407097e-17, 2.80731358e-02, 1.06082723},
},
FromBase: [3][3]float64{
{1.71666343, -0.35567332, -0.25336809},
{-0.66667384, 1.61645574, 0.0157683},
{0.01764248, -0.04277698, 0.94224328},
},
},
)
var Rec2020 = (&Space{
ID: "rec2020",
Name: "Rec. 2020",
Base: LinearRec2020,
Coords: RGBCoordinates,
ToBase: func(c [3]float64) [3]float64 {
f := func(v float64) float64 {
const b = 0.018053968510807
const a = 1 + 5.5*b
var sign float64
if v < 0 {
sign = -1
} else {
sign = 1
}
abs := math.Abs(v)
if abs < 4.5*b {
return v / 4.5
} else {
return sign * math.Pow(((abs+(a-1))/a), 1.0/0.45)
}
}
return [3]float64{
f(c[0]),
f(c[1]),
f(c[2]),
}
},
FromBase: func(c [3]float64) [3]float64 {
f := func(v float64) float64 {
const b = 0.018053968510807
const a = 1 + 5.5*b
var sign float64
if v < 0 {
sign = -1
} else {
sign = 1
}
abs := math.Abs(v)
if abs < b {
return 4.5 * v
} else {
return sign*a*math.Pow(abs, 0.45) - (a - 1)
}
}
return [3]float64{
f(c[0]),
f(c[1]),
f(c[2]),
}
},
}).Init()
var Rec2100PQ = (&Space{
ID: "rec2100-pq",
Name: "Rec. 2100 PQ",
Base: LinearRec2020,
Coords: RGBCoordinates,
ToBase: func(c [3]float64) [3]float64 {
f := func(v float64) float64 {
const m1 = 2610.0 / 16384.0
const m2 = 128 * (2523.0 / 4096.0)
const c3 = 32 * (2392.0 / 4096.0)
const c2 = 32 * (2413.0 / 4096.0)
const c1 = c3 - c2 + 1
EtoM2Recip := math.Pow(v, 1.0/m2)
nom := max(EtoM2Recip-c1, 0)
denom := c2 - c3*EtoM2Recip
return 10_000 * math.Pow(nom/denom, 1.0/m1)
}
return [3]float64{
f(c[0]) / 203,
f(c[1]) / 203,
f(c[2]) / 203,
}
},
FromBase: func(c [3]float64) [3]float64 {
f := func(v float64) float64 {
const m1 = 2610.0 / 16384.0
const m2 = 128 * (2523.0 / 4096.0)
const c3 = 32 * (2392.0 / 4096.0)
const c2 = 32 * (2413.0 / 4096.0)
const c1 = c3 - c2 + 1
Y := v / 10_000
YtoM1 := math.Pow(Y, m1)
return math.Pow((c1+c2*YtoM1)/(1+c3*YtoM1), m2)
}
return [3]float64{
f(c[0] * 203),
f(c[1] * 203),
f(c[2] * 203),
}
},
}).Init()
func mulVecMat(vec [3]float64, m *[3][3]float64) [3]float64 {
return [3]float64{
m[0][0]*vec[0] + m[0][1]*vec[1] + m[0][2]*vec[2],
m[1][0]*vec[0] + m[1][1]*vec[1] + m[1][2]*vec[2],
m[2][0]*vec[0] + m[2][1]*vec[1] + m[2][2]*vec[2],
}
}
func mulMatMat(m1, m2 *[3][3]float64) [3][3]float64 {
return [3][3]float64{
{
m1[0][0]*m2[0][0] + m1[0][1]*m2[1][0] + m1[0][2]*m2[2][0],
m1[0][0]*m2[0][1] + m1[0][1]*m2[1][1] + m1[0][2]*m2[2][1],
m1[0][0]*m2[0][2] + m1[0][1]*m2[1][2] + m1[0][2]*m2[2][2],
},
{
m1[1][0]*m2[0][0] + m1[1][1]*m2[1][0] + m1[1][2]*m2[2][0],
m1[1][0]*m2[0][1] + m1[1][1]*m2[1][1] + m1[1][2]*m2[2][1],
m1[1][0]*m2[0][2] + m1[1][1]*m2[1][2] + m1[1][2]*m2[2][2],
},
{
m1[2][0]*m2[0][0] + m1[2][1]*m2[1][0] + m1[2][2]*m2[2][0],
m1[2][0]*m2[0][1] + m1[2][1]*m2[1][1] + m1[2][2]*m2[2][1],
m1[2][0]*m2[0][2] + m1[2][1]*m2[1][2] + m1[2][2]*m2[2][2],
},
}
}