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
use std::ops::{Mul, MulAssign};

use crate::c::c_float;

/// RGBA F32 color that is byte-compatible with the Spine runtime.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[repr(C)]
pub struct Color {
    pub r: c_float,
    pub g: c_float,
    pub b: c_float,
    pub a: c_float,
}

impl Color {
    pub fn new_rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
        Self { r, g, b, a }
    }

    pub fn set_r(&mut self, r: c_float) -> &mut Self {
        self.r = r;
        self
    }

    pub fn set_g(&mut self, g: c_float) -> &mut Self {
        self.g = g;
        self
    }

    pub fn set_b(&mut self, b: c_float) -> &mut Self {
        self.b = b;
        self
    }

    pub fn set_a(&mut self, a: c_float) -> &mut Self {
        self.a = a;
        self
    }

    pub fn set_from_floats(&mut self, r: c_float, g: c_float, b: c_float, a: c_float) -> &mut Self {
        self.r = r;
        self.g = g;
        self.b = b;
        self.a = a;
        self.clamp();
        self
    }

    pub fn set_from_floats3(&mut self, r: c_float, g: c_float, b: c_float) -> &mut Self {
        self.r = r;
        self.g = g;
        self.b = b;
        self.clamp();
        self
    }

    pub fn set_from_color(&mut self, other: &Color) -> &mut Self {
        self.r = other.r;
        self.g = other.g;
        self.b = other.b;
        self.a = other.a;
        self
    }

    pub fn set_from_color3(&mut self, other: &Color) -> &mut Self {
        self.r = other.r;
        self.g = other.g;
        self.b = other.b;
        self
    }

    pub fn add_floats(&mut self, r: c_float, g: c_float, b: c_float, a: c_float) -> &mut Self {
        self.r += r;
        self.g += g;
        self.b += b;
        self.a += a;
        self.clamp();
        self
    }

    pub fn add_floats3(&mut self, r: c_float, g: c_float, b: c_float) -> &mut Self {
        self.r += r;
        self.g += g;
        self.b += b;
        self.clamp();
        self
    }

    pub fn add_color(&mut self, other: &Color) -> &mut Self {
        self.r += other.r;
        self.g += other.g;
        self.b += other.b;
        self.a += other.a;
        self.clamp();
        self
    }

    pub fn clamp(&mut self) -> &mut Self {
        self.r = self.r.clamp(0., 1.);
        self.g = self.r.clamp(0., 1.);
        self.b = self.r.clamp(0., 1.);
        self.a = self.r.clamp(0., 1.);
        self
    }

    pub fn premultiply_alpha(&mut self) {
        self.r *= self.a;
        self.g *= self.a;
        self.b *= self.a;
    }

    pub fn linear_to_nonlinear(&self) -> Color {
        Color {
            r: linear_to_nonlinear(self.r),
            g: linear_to_nonlinear(self.g),
            b: linear_to_nonlinear(self.b),
            a: self.a,
        }
    }

    pub fn nonlinear_to_linear(&self) -> Color {
        Color {
            r: nonlinear_to_linear(self.r),
            g: nonlinear_to_linear(self.g),
            b: nonlinear_to_linear(self.b),
            a: self.a,
        }
    }
}

impl Mul<Color> for Color {
    type Output = Color;

    fn mul(self, rhs: Color) -> Self::Output {
        Color {
            r: self.r * rhs.r,
            g: self.g * rhs.g,
            b: self.b * rhs.b,
            a: self.a * rhs.a,
        }
    }
}

impl MulAssign<Color> for Color {
    fn mul_assign(&mut self, rhs: Color) {
        self.r *= rhs.r;
        self.g *= rhs.g;
        self.b *= rhs.b;
        self.a *= rhs.a;
    }
}

fn linear_to_nonlinear(x: f32) -> f32 {
    if x <= 0.0 {
        x
    } else if x <= 0.0031308 {
        x * 12.92
    } else {
        (1.055 * x.powf(1.0 / 2.4)) - 0.055
    }
}

fn nonlinear_to_linear(x: f32) -> f32 {
    if x <= 0.0 {
        x
    } else if x <= 0.04045 {
        x / 12.92
    } else {
        ((x + 0.055) / 1.055).powf(2.4)
    }
}