-
Notifications
You must be signed in to change notification settings - Fork 1
/
transfer-function.go
73 lines (59 loc) · 1.91 KB
/
transfer-function.go
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
// Copyright (c) 2021 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package emission
import "math"
// TransferFunction is used to transform from device color spaces into linear device colors spaces, and vice versa.
type TransferFunction interface {
Linearize(v DCSVector) LinDCSVector
DeLinearize(v LinDCSVector) DCSVector
}
// transferFunctionStandardRGB implements the sRGB transfer function.
type transferFunctionStandardRGB struct{}
// TransferFunctionStandardRGB implements the sRGB transfer function.
var TransferFunctionStandardRGB = transferFunctionStandardRGB{}
func (tf transferFunctionStandardRGB) Linearize(vector DCSVector) LinDCSVector {
result := make(LinDCSVector, 0, vector.Channels())
for _, channel := range vector {
var trans float64
if channel <= 0.04045 {
trans = channel / 12.92
} else {
trans = math.Pow((channel+0.055)/1.055, 2.4)
}
result = append(result, trans)
}
return result
}
func (tf transferFunctionStandardRGB) DeLinearize(vector LinDCSVector) DCSVector {
result := make(DCSVector, 0, vector.Channels())
for _, channel := range vector {
var trans float64
if channel <= 0.0031308 {
trans = 12.92 * channel
} else {
trans = 1.055*math.Pow(channel, 1/2.4) - 0.055
}
result = append(result, trans)
}
return result
}
// TransferFunctionGamma implements the a gamma transfer function.
type TransferFunctionGamma struct {
Gamma float64
}
func (tf TransferFunctionGamma) Linearize(vector DCSVector) LinDCSVector {
result := make(LinDCSVector, 0, vector.Channels())
for _, channel := range vector {
result = append(result, math.Pow(channel, tf.Gamma))
}
return result
}
func (tf TransferFunctionGamma) DeLinearize(vector LinDCSVector) DCSVector {
result := make(DCSVector, 0, vector.Channels())
for _, channel := range vector {
result = append(result, math.Pow(channel, 1/tf.Gamma))
}
return result
}