Skip to content
Snippets Groups Projects
Verified Commit 5695ff97 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

chore: support for palettes in py

Makes use of hex based colors to simplify interface.
parent a31add2c
No related branches found
No related tags found
1 merge request!36Support for Python
Pipeline #3613 passed
This commit is part of merge request !36. Comments created here will be created in the context of that merge request.
...@@ -144,6 +144,24 @@ impl PaletteInfo { ...@@ -144,6 +144,24 @@ impl PaletteInfo {
} }
} }
pub fn from_colors_hex(name: &str, colors_hex: &str) -> Self {
let colors = Self::parse_colors_hex(colors_hex);
Self::new(name, colors)
}
pub fn parse_colors_hex(colors_hex: &str) -> Palette {
let mut colors = [[0u8; RGB_SIZE]; PALETTE_SIZE];
for (index, color) in colors_hex.split(',').enumerate() {
let color = color.trim();
let color = u32::from_str_radix(color, 16).unwrap_or(0);
let r = ((color >> 16) & 0xff) as u8;
let g = ((color >> 8) & 0xff) as u8;
let b = (color & 0xff) as u8;
colors[index] = [r, g, b];
}
colors
}
pub fn name(&self) -> &String { pub fn name(&self) -> &String {
&self.name &self.name
} }
...@@ -151,6 +169,24 @@ impl PaletteInfo { ...@@ -151,6 +169,24 @@ impl PaletteInfo {
pub fn colors(&self) -> &Palette { pub fn colors(&self) -> &Palette {
&self.colors &self.colors
} }
pub fn colors_hex(&self) -> String {
let mut buffer = String::new();
let mut is_first = true;
for color in self.colors.iter() {
let r = color[0];
let g = color[1];
let b = color[2];
let color = (r as u32) << 16 | (g as u32) << 8 | b as u32;
if is_first {
is_first = false;
} else {
buffer.push(',');
}
buffer.push_str(format!("{:06x}", color).as_str());
}
buffer
}
} }
/// Represents a tile within the Game Boy context, /// Represents a tile within the Game Boy context,
......
...@@ -3,7 +3,7 @@ use pyo3::{prelude::*, types::PyBytes}; ...@@ -3,7 +3,7 @@ use pyo3::{prelude::*, types::PyBytes};
use crate::{ use crate::{
gb::{GameBoy as GameBoyBase, GameBoyMode}, gb::{GameBoy as GameBoyBase, GameBoyMode},
info::Info, info::Info,
ppu::{DISPLAY_HEIGHT, DISPLAY_WIDTH}, ppu::{PaletteInfo, DISPLAY_HEIGHT, DISPLAY_WIDTH},
}; };
#[pyclass] #[pyclass]
...@@ -57,6 +57,11 @@ impl GameBoy { ...@@ -57,6 +57,11 @@ impl GameBoy {
pybytes.into() pybytes.into()
} }
pub fn set_palette_colors(&mut self, colors_hex: &str) {
let palette = PaletteInfo::from_colors_hex("default", colors_hex);
self.system.ppu().set_palette_colors(palette.colors());
}
pub fn ppu_enabled(&self) -> bool { pub fn ppu_enabled(&self) -> bool {
self.system.ppu_enabled() self.system.ppu_enabled()
} }
......
from .gb import * from .gb import (
DISPLAY_WIDTH,
DISPLAY_HEIGHT,
CPU_FREQ,
GameBoy,
GameBoyMode,
GameBoyRust,
)
from .palettes import PALETTES
...@@ -2,6 +2,8 @@ from enum import Enum ...@@ -2,6 +2,8 @@ from enum import Enum
from PIL.Image import Image, frombytes from PIL.Image import Image, frombytes
from .palettes import PALETTES
from .boytacean import DISPLAY_WIDTH, DISPLAY_HEIGHT, CPU_FREQ, GameBoy as GameBoyRust from .boytacean import DISPLAY_WIDTH, DISPLAY_HEIGHT, CPU_FREQ, GameBoy as GameBoyRust
...@@ -76,6 +78,15 @@ This is a [Game Boy](https://en.wikipedia.org/wiki/Game_Boy) emulator built usin ...@@ -76,6 +78,15 @@ This is a [Game Boy](https://en.wikipedia.org/wiki/Game_Boy) emulator built usin
image = self.image() image = self.image()
image.save(filename, format=format) image.save(filename, format=format)
def set_palette(self, name: str):
if not name in PALETTES:
raise ValueError(f"Unknown palette: {name}")
palette = PALETTES[name]
self.set_palette_colors(palette)
def set_palette_colors(self, colors_hex: str):
self._system.set_palette_colors(colors_hex)
@property @property
def ppu_enabled(self) -> bool: def ppu_enabled(self) -> bool:
return self._system.ppu_enabled() return self._system.ppu_enabled()
......
BASIC_PALETTE = "ffffff,c0c0c0,606060,000000"
HOGWARDS_PALETTE = "b6a571,8b7e56,554d35,201d13"
CHRISTMAS_PALETTE = "e8e7df,8bab95,9e5c5e,534d57"
GOLDSILVER_PALETTE = "c5c66d,97a1b0,585e67,235229"
PACMAN_PALETTE = "ffff00,ffb897,3732ff,000000"
MARIOBROS_PALETTE = "f7cec3,cc9e22,923404,000000"
POKEMON_PALETTE = "f87800,b86000,783800,000000"
PALETTES = {
"basic": BASIC_PALETTE,
"hogwards": HOGWARDS_PALETTE,
"christmas": CHRISTMAS_PALETTE,
"goldsilver": GOLDSILVER_PALETTE,
"pacman": PACMAN_PALETTE,
"mariobros": MARIOBROS_PALETTE,
"pokemon": POKEMON_PALETTE,
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment