rive_rs/math/
color.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::ops::{Mul, MulAssign};
6
7#[derive(Clone, Debug, PartialEq)]
8pub struct Color {
9    pub r: f32,
10    pub g: f32,
11    pub b: f32,
12    pub a: f32,
13}
14
15impl Color {
16    pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
17        Self { r, g, b, a }
18    }
19
20    pub fn lerp(self, other: Self, ratio: f32) -> Self {
21        Self {
22            r: self.r + (other.r - self.r) * ratio,
23            g: self.g + (other.g - self.g) * ratio,
24            b: self.b + (other.b - self.b) * ratio,
25            a: self.a + (other.a - self.a) * ratio,
26        }
27    }
28}
29
30impl Default for Color {
31    fn default() -> Self {
32        Self { r: 1.0, g: 1.0, b: 1.0, a: 1.0 }
33    }
34}
35
36impl Eq for Color {}
37
38impl Mul for Color {
39    type Output = Self;
40
41    fn mul(mut self, rhs: Self) -> Self::Output {
42        self.r *= rhs.r;
43        self.g *= rhs.g;
44        self.b *= rhs.b;
45        self.a *= rhs.a;
46
47        self
48    }
49}
50
51impl MulAssign for Color {
52    fn mul_assign(&mut self, rhs: Self) {
53        self.r *= rhs.r;
54        self.g *= rhs.g;
55        self.b *= rhs.b;
56        self.a *= rhs.a;
57    }
58}