ra2_pal/colors/
mod.rs

1use image::{Rgba,};
2use serde::{Deserialize, Serialize};
3
4/// The palette color used in game
5#[repr(C)]
6#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
7pub struct Ra2Color {
8    /// red 5 bits
9    pub red: u8,
10    /// green 6 bits
11    pub green: u8,
12    /// blue 5 bits
13    pub blue: u8,
14}
15
16impl From<Ra2Color> for Rgba<u8> {
17    fn from(value: Ra2Color) -> Self {
18        Rgba([
19            ((value.red as u32 * 255) / 63) as u8,
20            ((value.green as u32 * 255) / 63) as u8,
21            ((value.blue as u32 * 255) / 63) as u8,
22            255,
23        ])
24    }
25}
26
27impl From<Rgba<u8>> for Ra2Color {
28    fn from(value: Rgba<u8>) -> Self {
29        Self {
30            red: convert_8bit_to_5or6bit(value[0], false),
31            green: convert_8bit_to_5or6bit(value[1], true),
32            blue: convert_8bit_to_5or6bit(value[2], false),
33        }
34    }
35}
36
37/// 将 8 位颜色值转换为 5 位或 6 位颜色值 (分别用于红/蓝和绿)
38pub fn convert_8bit_to_5or6bit(color: u8, is_green: bool) -> u8 {
39    let divider = if is_green { 4 } else { 8 };
40    (color as u32 / divider) as u8
41}