Skip to content

Commit

Permalink
Merge pull request #107 from kriskw1999/main
Browse files Browse the repository at this point in the history
feat(hex input): added hex input for the colors
  • Loading branch information
RobDavenport committed Jun 14, 2024
2 parents ea64b33 + 01ca7b1 commit b83605c
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 6 deletions.
64 changes: 64 additions & 0 deletions gamercade_core/src/graphics/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,41 @@ impl Color {
r | g | b | a
}

pub fn to_hex_string(&self) -> String {
let r = self.r as u32;
let g = self.g as u32;
let b = self.b as u32;
format!("#{:02X}{:02X}{:02X}", r, g, b)
}

pub fn from_hex_string(hex: &str) -> Result<Self, &'static str> {
match (hex.starts_with('#'), hex.len()) {
(true, 7) => {
let hex_value =
u32::from_str_radix(&hex[1..], 16).map_err(|_| "Invalid hex string")?;
let r = ((hex_value >> 16) & 0xff) as u8;
let g = ((hex_value >> 8) & 0xff) as u8;
let b = (hex_value & 0xff) as u8;
return Ok(Self { r, g, b, a: 255 });
}
(false, 6) => {
let hex_value = u32::from_str_radix(&hex, 16).map_err(|_| "Invalid hex string")?;
let r = ((hex_value >> 16) & 0xff) as u8;
let g = ((hex_value >> 8) & 0xff) as u8;
let b = (hex_value & 0xff) as u8;
return Ok(Self { r, g, b, a: 255 });
}
_ => {
return Err("Invalid hex string");
}
}
}

pub fn update_from_hex_string(&mut self, hex: &str) -> Result<(), &'static str> {
*self = Self::from_hex_string(hex)?;
Ok(())
}

pub fn into_pixel_data(&self) -> [u8; BYTES_PER_PIXEL] {
[self.r, self.g, self.b, self.a]
}
Expand All @@ -91,3 +126,32 @@ impl From<[u8; 4]> for Color {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_color_from_hex_string() {
let color = Color::from_hex_string("#FF0000").unwrap();
assert_eq!(color, Color::new(255, 0, 0, 255));

let color = Color::from_hex_string("#000000").unwrap();
assert_eq!(color, Color::new(0, 0, 0, 255));

let color = Color::from_hex_string("FF0000").unwrap();
assert_eq!(color, Color::new(255, 0, 0, 255));

let color = Color::from_hex_string("ffffff").unwrap();
assert_eq!(color, Color::new(255, 255, 255, 255));

assert!(Color::from_hex_string("#FF000").is_err());
assert!(Color::from_hex_string("#FF00000").is_err());
assert!(Color::from_hex_string("#FF00000FF").is_err());
assert!(Color::from_hex_string("FF00000FF").is_err());
assert!(Color::from_hex_string("asd").is_err());
assert!(Color::from_hex_string("pppppppp").is_err());
assert!(Color::from_hex_string("#pppppp").is_err());
assert!(Color::from_hex_string("#FFFFFFFF").is_err());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use eframe::egui::{Checkbox, Color32, Image, Slider, TextureHandle, Ui, Vec2};
use eframe::egui::{Checkbox, Color32, Image, Slider, TextEdit, TextureHandle, Ui, Vec2};
use gamercade_core::Color;

#[derive(Clone, Debug, Default)]
pub struct ColorEditor {
prev_color: Color,
pub preview: Color,
pub hex_preview: String,
}

impl ColorEditor {
Expand All @@ -18,25 +19,44 @@ impl ColorEditor {
if self.prev_color != *current_color {
self.preview = *current_color;
self.prev_color = *current_color;
self.hex_preview = (*current_color).to_hex_string();
}

let mut current_hex = (*current_color).to_hex_string();

ui.group(|ui| {
ui.vertical(|ui| {
ui.label("Color Editor");

ui.label(format!("Color index: {index}"));

draw_picker(ui, texture_handle, "Current", false, current_color);
draw_picker(ui, texture_handle, "Preview", true, &mut self.preview);
draw_picker(
ui,
texture_handle,
"Current",
false,
current_color,
&mut current_hex,
);
draw_picker(
ui,
texture_handle,
"Preview",
true,
&mut self.preview,
&mut self.hex_preview,
);

ui.horizontal(|ui| {
if ui.button("Revert").clicked() {
self.preview = *current_color;
self.hex_preview = (*current_color).to_hex_string();
}

if ui.button("Update").clicked() {
*current_color = self.preview;
self.prev_color = self.preview;
self.hex_preview = current_hex;
}
})
});
Expand All @@ -50,6 +70,7 @@ fn draw_picker(
text: &'static str,
editable: bool,
color: &mut Color,
hex_color: &mut String,
) {
ui.group(|ui| {
ui.label(text);
Expand All @@ -60,17 +81,45 @@ fn draw_picker(
ui.vertical(|ui| {
ui.horizontal(|ui| {
ui.label("R");
ui.add_enabled(editable, Slider::new(&mut color.r, 0..=255));
if ui
.add_enabled(editable, Slider::new(&mut color.r, 0..=255))
.changed()
{
*hex_color = color.to_hex_string();
}
});

ui.horizontal(|ui| {
ui.label("G");
ui.add_enabled(editable, Slider::new(&mut color.g, 0..=255));
if ui
.add_enabled(editable, Slider::new(&mut color.g, 0..=255))
.changed()
{
*hex_color = color.to_hex_string();
}
});

ui.horizontal(|ui| {
ui.label("B");
ui.add_enabled(editable, Slider::new(&mut color.b, 0..=255));
if ui
.add_enabled(editable, Slider::new(&mut color.b, 0..=255))
.changed()
{
*hex_color = color.to_hex_string();
}
});

ui.horizontal(|ui| {
ui.label("Hex");
if ui
.add_enabled(
editable,
TextEdit::singleline(hex_color).desired_width(64.0),
)
.changed()
{
let _result = color.update_from_hex_string(hex_color);
}
});

ui.horizontal(|ui| {
Expand Down

0 comments on commit b83605c

Please sign in to comment.