[cln] Code cleanup

This commit is contained in:
2024-02-19 23:03:33 +05:30
parent 7e704ff982
commit f523ee91ff
9 changed files with 34 additions and 114 deletions

View File

@@ -1,5 +1,3 @@
use crate::misc::error::EmulatorError;
use crate::misc::result::EmulatorResult;
/// A Color as represented in BytePusher.
/// It is a byte that contains a packed 6 digit red, green and blue
@@ -15,31 +13,15 @@ impl Color {
/// Only first 216 color indices are used.
const COLOR_MAX: u8 = 215;
const COLOR_FACTOR_8_BIT: u8 = 0x33;
/// This constructs a valid color from rgb triplet
pub fn from_rgb(red: u8, green: u8, blue: u8) -> Color {
let red = red / Self::COLOR_FACTOR_8_BIT;
let green = green / Self::COLOR_FACTOR_8_BIT;
let blue = blue / Self::COLOR_FACTOR_8_BIT;
Color(red * Self::RED_MULT + green * Self::GREEN_MULT + blue)
}
pub fn try_new(in_mem_color: u8) -> EmulatorResult<Color> {
if in_mem_color > Self::COLOR_MAX {
return Err(EmulatorError::InvalidColor(in_mem_color));
}
Ok(Color(in_mem_color))
}
/// wrap to black if needed
pub fn new(in_mem_color: u8) -> Color {
if in_mem_color > Self::COLOR_MAX {
log::trace!("Invalid color {}, using 0", in_mem_color);
Color(0)
} else {
Color(in_mem_color)
}
}
pub fn get_mem_byte(&self) -> u8 {
self.0
}
/// This fetches the rgb triplet
pub fn get_rgb(self) -> (u8, u8, u8) {
let r = self.0 / Self::RED_MULT;
@@ -48,16 +30,6 @@ impl Color {
let b = gb_byte_remainder % Self::GREEN_MULT;
(r * Self::COLOR_FACTOR_8_BIT, g * Self::COLOR_FACTOR_8_BIT, b * Self::COLOR_FACTOR_8_BIT)
}
pub fn to_rgb_555(&self)->u16{
let r = (self.0 / Self::RED_MULT) as u16;
let gb_byte_remainder = self.0 % Self::RED_MULT;
let g = (gb_byte_remainder / Self::GREEN_MULT) as u16;
let b = gb_byte_remainder % Self::GREEN_MULT;
(r*25)<<10 + (g*25)<<5 + b
}
}
@@ -67,31 +39,20 @@ mod tests {
#[test]
pub fn test_from_mem_zero() {
let color = Color::try_new(0).unwrap();
let color = Color::new(0);
assert_eq!((0, 0, 0), color.get_rgb())
}
#[test]
pub fn test_from_mem_invalid() {
let color = Color::try_new(0xff);
assert!(color.is_err())
let color = Color::new(0xff);
assert_eq!((0,0,0),color.get_rgb())
}
#[test]
pub fn test_from_mem_max() {
let color = Color::try_new(Color::COLOR_MAX).unwrap();
let color = Color::new(Color::COLOR_MAX);
assert_eq!((255, 255, 255), color.get_rgb())
}
#[test]
pub fn from_rgb_zero() {
let color = Color::from_rgb(0, 0, 0);
assert_eq!(0, color.get_mem_byte())
}
#[test]
pub fn from_rgb_max() {
let color = Color::from_rgb(255, 255, 255);
assert_eq!(Color::COLOR_MAX, color.get_mem_byte())
}
}

View File

@@ -1,12 +1,9 @@
use std::cell::Ref;
use std::fmt::{Debug, Formatter};
use std::mem::size_of;
use sdl2::pixels::PixelFormatEnum;
use sdl2::rect::Rect;
use sdl2::render::{TextureAccess, WindowCanvas};
use crate::emu::graphics::{DEVICE_FRAMEBUFFER_SIZE, GraphicsProcessor};
use crate::graphics::color::Color;
use crate::misc::error::EmulatorError;
use crate::misc::result::EmulatorResult;