implements PLN-0068

This commit is contained in:
2026-05-23 19:19:28 +01:00
parent 1256d33a72
commit 668a061964
12 changed files with 121 additions and 121 deletions
+3 -3
View File
@@ -658,7 +658,7 @@ impl AssetManager {
let offset = (p * 16 + c) * 2;
let color_raw =
u16::from_le_bytes([palette_data[offset], palette_data[offset + 1]]);
*slot = Color(color_raw);
*slot = Color::from_rgb565_lossy(color_raw);
}
}
@@ -690,7 +690,7 @@ impl AssetManager {
let offset = (p * 16 + c) * 2;
let color_raw =
u16::from_le_bytes([palette_data[offset], palette_data[offset + 1]]);
*slot = Color(color_raw);
*slot = Color::from_rgb565_lossy(color_raw);
}
}
@@ -1313,7 +1313,7 @@ mod tests {
AssetManager::decode_glyph_bank_from_buffer(&entry, &data).expect("glyph decode");
assert_eq!(bank.pixel_indices, vec![1, 0, 2, 3]);
assert_eq!(bank.palettes[0][0], Color(0x1234));
assert_eq!(bank.palettes[0][0], Color::from_rgb565_lossy(0x1234));
}
#[test]
+37 -26
View File
@@ -381,7 +381,7 @@ impl Gfx {
}
pub fn clear(&mut self, color: Color) {
self.back.fill(color.0);
self.back.fill(color.to_rgb565_lossy());
}
/// Rectangle with blend mode.
@@ -406,7 +406,7 @@ impl Gfx {
let x1 = (x + w).clamp(0, fw);
let y1 = (y + h).clamp(0, fh);
let src = color.0;
let src = color.to_rgb565_lossy();
for yy in y0..y1 {
let row = (yy as usize) * self.w;
@@ -429,7 +429,7 @@ impl Gfx {
return;
}
if x >= 0 && x < self.w as i32 && y >= 0 && y < self.h as i32 {
self.back[y as usize * self.w + x as usize] = color.0;
self.back[y as usize * self.w + x as usize] = color.to_rgb565_lossy();
}
}
@@ -582,7 +582,7 @@ impl Gfx {
return;
}
for x in start..=end {
self.back[y as usize * self.w + x as usize] = color.0;
self.back[y as usize * self.w + x as usize] = color.to_rgb565_lossy();
}
}
@@ -600,7 +600,7 @@ impl Gfx {
return;
}
for y in start..=end {
self.back[y as usize * self.w + x as usize] = color.0;
self.back[y as usize * self.w + x as usize] = color.to_rgb565_lossy();
}
}
@@ -657,7 +657,7 @@ impl Gfx {
update: &ResolverUpdate,
resolved_glyph_slots: &[usize; 4],
) {
self.back.fill(Color::BLACK.raw());
self.back.fill(Color::BLACK.to_rgb565_lossy());
self.populate_layer_buckets();
for (layer_index, glyph_slot) in
@@ -779,7 +779,7 @@ impl Gfx {
}
let color = tile.bank.resolve_color(tile.entry.palette_id, px_index);
target.back[world_y as usize * target.screen_w + world_x as usize] = color.raw();
target.back[world_y as usize * target.screen_w + world_x as usize] = color.to_rgb565_lossy();
}
}
}
@@ -834,7 +834,7 @@ impl Gfx {
// 3. Resolve color via palette (from the tile inside the sprite)
let color = bank.resolve_color(sprite.glyph.palette_id, px_index);
back[world_y as usize * screen_w + world_x as usize] = color.raw();
back[world_y as usize * screen_w + world_x as usize] = color.to_rgb565_lossy();
}
}
}
@@ -848,17 +848,17 @@ impl Gfx {
let weight = level as u16;
let inv_weight = 31 - weight;
let (fr, fg, fb) = Color::unpack_to_native(fade_color.0);
let (fr, fg, fb) = unpack_rgb565_native(fade_color.to_rgb565_lossy());
for px in back.iter_mut() {
let (sr, sg, sb) = Color::unpack_to_native(*px);
let (sr, sg, sb) = unpack_rgb565_native(*px);
// Formula: (src * weight + fade * inv_weight) / 31
let r = ((sr as u16 * weight + fr as u16 * inv_weight) / 31) as u8;
let g = ((sg as u16 * weight + fg as u16 * inv_weight) / 31) as u8;
let b = ((sb as u16 * weight + fb as u16 * inv_weight) / 31) as u8;
*px = Color::pack_from_native(r, g, b);
*px = pack_rgb565_native(r, g, b);
}
}
@@ -900,7 +900,7 @@ impl Gfx {
}
let glyph = glyph_for_char(c);
let raw = color.0;
let raw = color.to_rgb565_lossy();
let row_start = (-y).clamp(0, glyph_h) as usize;
let row_end = (screen_h - y).clamp(0, glyph_h) as usize;
@@ -932,49 +932,60 @@ fn blend_rgb565(dst: u16, src: u16, mode: BlendMode) -> u16 {
BlendMode::None => src,
BlendMode::Half => {
let (dr, dg, db) = Color::unpack_to_native(dst);
let (sr, sg, sb) = Color::unpack_to_native(src);
let (dr, dg, db) = unpack_rgb565_native(dst);
let (sr, sg, sb) = unpack_rgb565_native(src);
let r = ((dr as u16 + sr as u16) >> 1) as u8;
let g = ((dg as u16 + sg as u16) >> 1) as u8;
let b = ((db as u16 + sb as u16) >> 1) as u8;
Color::pack_from_native(r, g, b)
pack_rgb565_native(r, g, b)
}
BlendMode::HalfPlus => {
let (dr, dg, db) = Color::unpack_to_native(dst);
let (sr, sg, sb) = Color::unpack_to_native(src);
let (dr, dg, db) = unpack_rgb565_native(dst);
let (sr, sg, sb) = unpack_rgb565_native(src);
let r = (dr as u16 + ((sr as u16) >> 1)).min(31) as u8;
let g = (dg as u16 + ((sg as u16) >> 1)).min(63) as u8;
let b = (db as u16 + ((sb as u16) >> 1)).min(31) as u8;
Color::pack_from_native(r, g, b)
pack_rgb565_native(r, g, b)
}
BlendMode::HalfMinus => {
let (dr, dg, db) = Color::unpack_to_native(dst);
let (sr, sg, sb) = Color::unpack_to_native(src);
let (dr, dg, db) = unpack_rgb565_native(dst);
let (sr, sg, sb) = unpack_rgb565_native(src);
let r = (dr as i16 - ((sr as i16) >> 1)).max(0) as u8;
let g = (dg as i16 - ((sg as i16) >> 1)).max(0) as u8;
let b = (db as i16 - ((sb as i16) >> 1)).max(0) as u8;
Color::pack_from_native(r, g, b)
pack_rgb565_native(r, g, b)
}
BlendMode::Full => {
let (dr, dg, db) = Color::unpack_to_native(dst);
let (sr, sg, sb) = Color::unpack_to_native(src);
let (dr, dg, db) = unpack_rgb565_native(dst);
let (sr, sg, sb) = unpack_rgb565_native(src);
let r = (dr as u16 + sr as u16).min(31) as u8;
let g = (dg as u16 + sg as u16).min(63) as u8;
let b = (db as u16 + sb as u16).min(31) as u8;
Color::pack_from_native(r, g, b)
pack_rgb565_native(r, g, b)
}
}
}
fn unpack_rgb565_native(px: u16) -> (u8, u8, u8) {
let r = ((px >> 11) & 0x1F) as u8;
let g = ((px >> 5) & 0x3F) as u8;
let b = (px & 0x1F) as u8;
(r, g, b)
}
fn pack_rgb565_native(r: u8, g: u8, b: u8) -> u16 {
((r as u16 & 0x1F) << 11) | ((g as u16 & 0x3F) << 5) | (b as u16 & 0x1F)
}
#[cfg(test)]
mod tests {
use super::*;
@@ -1133,7 +1144,7 @@ mod tests {
gfx.hud_fade_level = 31;
gfx.render_scene_from_cache(&cache, &update, &[0, 0, 0, 0]);
assert_eq!(gfx.back[0], Color::RED.raw());
assert_eq!(gfx.back[0], Color::RED.to_rgb565_lossy());
}
#[test]
@@ -1184,7 +1195,7 @@ mod tests {
gfx.render_scene_from_cache(&cache, &update, &[0, 0, 0, 0]);
assert_eq!(gfx.back[0], Color::BLUE.raw());
assert_eq!(gfx.back[0], Color::BLUE.to_rgb565_lossy());
}
#[test]