summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 90c9445e9b9a494dc567afc881ea5460c6a8bdce (plain) (blame)
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
use proportion::*;


#[derive(Copy, Clone, Default, PartialEq, Eq, Hash)]
pub struct Colour {
    /// Colour encoded as `0xAARRGGBB`.
    pub value: u32,
}

impl Colour {
    /// Construct a Colour from an RGB u32.
    pub const fn rgb(value: u32) -> Self {
        Self {
            value: value | 0xff000000,
        }
    }

    /// Construct a Colour from an RGBA u32.
    pub const fn rgba(value: u32) -> Self {
        Self { value }
    }

    /// Construct a Colour from a brightness value.
    pub const fn grey(value: u32) -> Self {
        Self::rgb((value & 0xf) * 0x111111)
    }

    /// Construct a Colour from separate red, green, and blue channel values.
    pub const fn from_rgb(red: u8, green: u8, blue: u8) -> Self {
        let mut value = 0x0000ff00;
        value |= red as u32;
        value <<= 8; value |= green as u32;
        value <<= 8; value |= blue as u32;
        Self { value }
    }

    /// Construct a Colour from separate red, green, blue, and alpha channel values.
    pub const fn from_rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
        let mut value = alpha  as u32;
        value <<= 8; value |= red   as u32;
        value <<= 8; value |= green as u32;
        value <<= 8; value |= blue  as u32;
        Self { value }
    }

    /// Construct a colour by blending two colours by a proportion.
    pub fn mix<I: proportion::Internal>(bg: Self, fg: Self, proportion: Proportion<I>) -> Self {
        let proportion = Proportion::<u8>::new(proportion.as_u8());
        let bg_mix = proportion.invert();
        let fg_mix = proportion;
        let red = (bg.red() * bg_mix) + (fg.red() * fg_mix);
        let green = (bg.green() * bg_mix) + (fg.green() * fg_mix);
        let blue = (bg.blue() * bg_mix) + (fg.blue() * fg_mix);
        Self::from_rgb(red, green, blue)
    }

    /// Construct a colour by blending two colours by the alpha of the fg colour.
    pub fn mix_with_alpha(bg: Self, fg: Self) -> Self {
        let proportion = Proportion::new(fg.alpha());
        let bg_mix = proportion.invert();
        let fg_mix = proportion;
        let red = (bg.red() * bg_mix) + (fg.red() * fg_mix);
        let green = (bg.green() * bg_mix) + (fg.green() * fg_mix);
        let blue = (bg.blue() * bg_mix) + (fg.blue() * fg_mix);
        Self::from_rgb(red, green, blue)
    }

    /// Get an array of red, green, and blue channel values.
    pub fn as_rgb_array(&self) -> [u8; 3] {
        [self.red(), self.green(), self.blue()]
    }

    /// Get an array of red, green, blue, and alpha channel values.
    pub fn as_rgba_array(&self) -> [u8; 4] {
        [self.red(), self.green(), self.blue(), self.alpha()]
    }

    pub fn as_rgb_hex(&self) -> String {
        format!("{:02x}{:02x}{:02x}",
            self.red(), self.green(), self.blue())
    }

    pub fn as_rgba_hex(&self) -> String {
        format!("{:02x}{:02x}{:02x}{:02x}",
            self.red(), self.green(), self.blue(), self.alpha())
    }

    pub fn red(&self) -> u8 {
        (self.value >> 16) as u8
    }

    pub fn green(&self) -> u8 {
        (self.value >> 8) as u8
    }

    pub fn blue(&self) -> u8 {
        self.value as u8
    }

    pub fn alpha(&self) -> u8 {
        (self.value >> 24) as u8
    }

    pub fn is_opaque(&self) -> bool {
        self.alpha() == 0xff
    }

    pub fn with_red(&self, red: u8) -> Self {
        Colour::rgba((self.value & 0xffffff00) | (red as u32) << 16)
    }

    pub fn with_green(&self, green: u8) -> Self {
        Colour::rgba((self.value & 0xffff00ff) | (green as u32) << 8)
    }

    pub fn with_blue(&self, blue: u8) -> Self {
        Colour::rgba((self.value & 0xff00ffff) | blue as u32)
    }

    pub fn with_alpha(&self, alpha: u8) -> Self {
        Colour::rgba((self.value & 0x00ffffff) | (alpha as u32) << 24)
    }

    pub fn set_red(&mut self, red: u8) {
        self.value = (self.value & 0xffffff00) | (red as u32) << 16
    }

    pub fn set_green(&mut self, green: u8) {
        self.value = (self.value & 0xffff00ff) | (green as u32) << 8
    }

    pub fn set_blue(&mut self, blue: u8) {
        self.value = (self.value & 0xff00ffff) | blue as u32
    }

    pub fn set_alpha(&mut self, alpha: u8) {
        self.value = (self.value & 0x00ffffff) | (alpha as u32) << 24
    }
}

impl Colour {
    pub const TRANSPARENT: Self = Self::rgba(0x00000000);
    pub const WHITE: Self = Self::rgb(0xffffff);
    pub const BLACK: Self = Self::rgb(0x000000);

    pub const LIGHT_GREY: Self = Self::rgb(0xc0c0c0);
    pub const MID_GREY: Self = Self::rgb(0x808080);
    pub const DARK_GREY: Self = Self::rgb(0x404040);

    pub const RED: Self = Self::rgb(0xff0000);
    pub const GREEN: Self = Self::rgb(0x00ff00);
    pub const BLUE: Self = Self::rgb(0x0000ff);

    pub const YELLOW: Self = Self::rgb(0xffff00);
    pub const MAGENTA: Self = Self::rgb(0xff00ff);
    pub const CYAN: Self = Self::rgb(0x00ffff);

    pub const ORANGE: Self = Self::rgb(0xff8000);
    pub const PURPLE: Self = Self::rgb(0x800080);
    pub const TEAL: Self = Self::rgb(0x008080);
}

impl From<u32> for Colour {
    fn from(value: u32) -> Self {
        Colour { value }
    }
}

impl From<Colour> for u32 {
    fn from(colour: Colour) -> Self {
        colour.value
    }
}

impl AsRef<u32> for Colour {
    fn as_ref(&self) -> &u32 {
        &self.value
    }
}

impl std::fmt::Debug for Colour {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(f,
            "Colour {{ r:{} g:{} b:{}, a:{} }}",
            self.red(),
            self.green(),
            self.blue(),
            self.alpha()
        )
    }
}

impl std::fmt::Display for Colour {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(f,
            "#{:02x}{:02x}{:02x}{:02x}",
            self.red(),
            self.green(),
            self.blue(),
            self.alpha()
        )
    }
}