|
1 | 1 | //! |
2 | 2 | //! This file provides common color types found on the supported displays. |
3 | 3 | //! |
| 4 | +//! |
| 5 | +const std = @import("std"); |
4 | 6 |
|
5 | 7 | /// A color type encoding only black and white. |
6 | 8 | pub const BlackWhite = enum(u1) { |
7 | 9 | black = 0, |
8 | 10 | white = 1, |
9 | 11 | }; |
10 | 12 |
|
11 | | -pub const RGB565 = Color(.{ u5, u6, u5 }); |
12 | | -pub const RGB888 = Color(.{ u8, u8, u8 }); |
13 | | - |
14 | | -/// Provides a namespace with the default colors for the given `Color` type. |
15 | | -pub fn Color(comptime Ts: [3]type) type { |
16 | | - return packed struct { |
| 13 | +pub fn RGB565_Generic(desired_endianness: std.builtin.Endian) type { |
| 14 | + return struct { |
17 | 15 | const Self = @This(); |
18 | 16 |
|
19 | | - r: Ts[0], |
20 | | - g: Ts[1], |
21 | | - b: Ts[2], |
| 17 | + value: u16, |
22 | 18 |
|
23 | | - pub const black: Self = .from_rgb(0x00, 0x00, 0x00); |
24 | | - pub const white: Self = .from_rgb(0xFF, 0xFF, 0xFF); |
25 | | - pub const red: Self = .from_rgb(0xFF, 0x00, 0x00); |
26 | | - pub const green: Self = .from_rgb(0x00, 0xFF, 0x00); |
27 | | - pub const blue: Self = .from_rgb(0x00, 0x00, 0xFF); |
28 | | - pub const cyan: Self = .from_rgb(0x00, 0xFF, 0xFF); |
29 | | - pub const magenta: Self = .from_rgb(0xFF, 0x00, 0xFF); |
30 | | - pub const yellow: Self = .from_rgb(0xFF, 0xFF, 0x00); |
| 19 | + pub const black: Self = from_rgb(0x00, 0x00, 0x00); |
| 20 | + pub const white: Self = from_rgb(0xFF, 0xFF, 0xFF); |
| 21 | + pub const red: Self = from_rgb(0xFF, 0x00, 0x00); |
| 22 | + pub const green: Self = from_rgb(0x00, 0xFF, 0x00); |
| 23 | + pub const blue: Self = from_rgb(0x00, 0x00, 0xFF); |
| 24 | + pub const cyan: Self = from_rgb(0x00, 0xFF, 0xFF); |
| 25 | + pub const magenta: Self = from_rgb(0xFF, 0x00, 0xFF); |
| 26 | + pub const yellow: Self = from_rgb(0xFF, 0xFF, 0x00); |
31 | 27 |
|
32 | 28 | pub fn from_rgb(r: u8, g: u8, b: u8) Self { |
33 | | - return Self{ |
34 | | - .r = @truncate(r >> (8 - @bitSizeOf(Ts[0]))), |
35 | | - .g = @truncate(g >> (8 - @bitSizeOf(Ts[1]))), |
36 | | - .b = @truncate(b >> (8 - @bitSizeOf(Ts[2]))), |
37 | | - }; |
| 29 | + const r5: u16 = @as(u16, r) >> 3; |
| 30 | + const g6: u16 = @as(u16, g) >> 2; |
| 31 | + const b5: u16 = @as(u16, b) >> 3; |
| 32 | + |
| 33 | + const v: u16 = (r5 << 11) | (g6 << 5) | b5; |
| 34 | + |
| 35 | + return .{ .value = std.mem.nativeTo(u16, v, desired_endianness) }; |
38 | 36 | } |
39 | 37 | }; |
40 | 38 | } |
0 commit comments