Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change internal representation of Color to f32 #69

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 49 additions & 49 deletions src/graphics/color.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
use procedural::*;
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Color {
pub red: u8,
pub blue: u8,
pub green: u8,
pub alpha: u8,
pub red: f32,
pub blue: f32,
pub green: f32,
pub alpha: f32,
}

impl Color {
pub const fn rgb(red: u8, green: u8, blue: u8) -> Self {
pub const fn rgb(red: f32, green: f32, blue: f32) -> Self {
Self {
red,
green,
blue,
alpha: 255,
alpha: 1.0,
}
}

pub const fn rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
pub const fn rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self { red, green, blue, alpha }
}

pub fn rgb_f32(red: f32, green: f32, blue: f32) -> Self {
let red = (red * 255.0) as u8;
let green = (green * 255.0) as u8;
let blue = (blue * 255.0) as u8;
pub fn rgb_u8(red: u8, green: u8, blue: u8) -> Self {
let red = (red as f32) / 255.0;
let green = (green as f32) / 255.0;
let blue = (blue as f32) / 255.0;

Self {
red,
green,
blue,
alpha: 255,
alpha: 1.0,
}
}

pub fn rgba_f32(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
let red = (red * 255.0) as u8;
let green = (green * 255.0) as u8;
let blue = (blue * 255.0) as u8;
let alpha = (alpha * 255.0) as u8;
pub fn rgba_u8(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
let red = (red as f32) / 255.0;
let green = (green as f32) / 255.0;
let blue = (blue as f32) / 255.0;
let alpha = (alpha as f32) / 255.0;

Self { red, green, blue, alpha }
}
Expand All @@ -49,72 +49,72 @@ impl Color {
assert_eq!(hex.len(), 6);

let channel = |range| u8::from_str_radix(&hex[range], 16).unwrap();
Color::rgb(channel(0..2), channel(2..4), channel(4..6))
Color::rgb_u8(channel(0..2), channel(2..4), channel(4..6))
}

pub const fn monochrome(brightness: u8) -> Self {
pub fn monochrome_u8(brightness: u8) -> Self {
let brightness = (brightness as f32) / 255.0;
Self {
red: brightness,
green: brightness,
blue: brightness,
alpha: 255,
alpha: 1.0,
}
}

pub fn red_f32(&self) -> f32 {
self.red as f32 / 255.0
pub fn red_as_u8(&self) -> u8 {
(self.red * 255.0) as u8
}

pub fn green_f32(&self) -> f32 {
self.green as f32 / 255.0
pub fn green_as_u8(&self) -> u8 {
(self.green * 255.0) as u8
}

pub fn blue_f32(&self) -> f32 {
self.blue as f32 / 255.0
pub fn blue_as_u8(&self) -> u8 {
(self.blue * 255.0) as u8
}

pub fn alpha_f32(&self) -> f32 {
self.alpha as f32 / 255.0
pub fn alpha_as_u8(&self) -> u8 {
(self.alpha * 255.0) as u8
}

#[cfg(feature = "debug")]
pub fn multiply_alpha_f32(mut self, alpha: f32) -> Self {
let new_alpha = alpha * self.alpha_f32();
self.alpha = (new_alpha * 255.0) as u8;
pub fn multiply_alpha(mut self, alpha: f32) -> Self {
self.alpha *= alpha;
self
}

pub fn invert(&self) -> Self {
Self::rgba(255 - self.red, 255 - self.green, 255 - self.green, self.alpha)
Self::rgba(1.0 - self.red, 1.0 - self.blue, 1.0 - self.green, self.alpha)
}

pub fn shade(&self) -> Self {
match (self.red as usize) + (self.green as usize) + (self.blue as usize) > 382 {
true => Self::rgba(
self.red.saturating_sub(40),
self.green.saturating_sub(40),
self.blue.saturating_sub(40),
self.alpha,
match (self.red_as_u8() as usize) + (self.green_as_u8() as usize) + (self.blue_as_u8() as usize) > 382 {
true => Self::rgba_u8(
self.red_as_u8().saturating_sub(40),
self.green_as_u8().saturating_sub(40),
self.blue_as_u8().saturating_sub(40),
self.alpha_as_u8(),
),
false => Self::rgba(
self.red.saturating_add(40),
self.green.saturating_add(40),
self.blue.saturating_add(40),
self.alpha,
false => Self::rgba_u8(
self.red_as_u8().saturating_add(40),
self.green_as_u8().saturating_add(40),
self.blue_as_u8().saturating_add(40),
self.alpha_as_u8(),
),
}
}
}

impl From<Color> for [f32; 3] {
fn from(val: Color) -> Self {
[val.red_f32(), val.green_f32(), val.blue_f32()]
[val.red, val.green, val.blue]
}
}

impl From<Color> for [f32; 4] {
fn from(val: Color) -> Self {
[val.red_f32(), val.green_f32(), val.blue_f32(), val.alpha_f32()]
[val.red, val.green, val.blue, val.alpha]
}
}

Expand All @@ -129,7 +129,7 @@ pub struct ColorBGRA {

impl From<ColorBGRA> for Color {
fn from(color: ColorBGRA) -> Self {
Self::rgba(color.red, color.green, color.blue, color.alpha)
Self::rgba_u8(color.red, color.green, color.blue, color.alpha)
}
}

Expand All @@ -143,7 +143,7 @@ pub struct ColorRGB {

impl From<ColorRGB> for Color {
fn from(color: ColorRGB) -> Self {
Self::rgb_f32(color.red, color.green, color.blue)
Self::rgb(color.red, color.green, color.blue)
}
}

Expand All @@ -158,6 +158,6 @@ pub struct ColorRGBA {

impl From<ColorRGBA> for Color {
fn from(color: ColorRGBA) -> Self {
Self::rgba(color.red, color.green, color.blue, color.alpha)
Self::rgba_u8(color.red, color.green, color.blue, color.alpha)
}
}
24 changes: 18 additions & 6 deletions src/graphics/particles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ impl Particle for DamageNumber {
top: screen_position.y * window_size.height,
};

renderer.render_damage_text(render_target, &self.damage_amount, final_position, Color::monochrome(255), 16.0);
renderer.render_damage_text(
render_target,
&self.damage_amount,
final_position,
Color::monochrome_u8(255),
16.0,
);
}
}

Expand Down Expand Up @@ -111,7 +117,13 @@ impl Particle for HealNumber {
top: screen_position.y * window_size.height,
};

renderer.render_damage_text(render_target, &self.heal_amount, final_position, Color::rgb(30, 255, 30), 16.0);
renderer.render_damage_text(
render_target,
&self.heal_amount,
final_position,
Color::rgb_u8(30, 255, 30),
16.0,
);
}
}

Expand All @@ -137,10 +149,10 @@ impl QuestIcon {
)
.unwrap();
let color = match quest_effect.color {
QuestColor::Yellow => Color::rgb(200, 200, 30),
QuestColor::Orange => Color::rgb(200, 100, 30),
QuestColor::Green => Color::rgb(30, 200, 30),
QuestColor::Purple => Color::rgb(200, 30, 200),
QuestColor::Yellow => Color::rgb_u8(200, 200, 30),
QuestColor::Orange => Color::rgb_u8(200, 100, 30),
QuestColor::Green => Color::rgb_u8(30, 200, 30),
QuestColor::Purple => Color::rgb_u8(200, 30, 200),
};

Self { position, texture, color }
Expand Down
6 changes: 1 addition & 5 deletions src/graphics/renderers/deferred/directional/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ impl DirectionalLightRenderer {

let constants = Constants {
direction: Padded(direction.into()),
color: [
color.red_f32() * intensity,
color.green_f32() * intensity,
color.blue_f32() * intensity,
],
color: [color.red * intensity, color.green * intensity, color.blue * intensity],
};

render_target
Expand Down
20 changes: 10 additions & 10 deletions src/graphics/renderers/deferred/sprite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,16 @@ impl SpriteRenderer {
hovered: bool,
) {
let (texture, color) = match marker_identifier {
MarkerIdentifier::Object(..) if hovered => (self.object_marker_texture.clone(), Color::rgb(235, 180, 52)),
MarkerIdentifier::Object(..) => (self.object_marker_texture.clone(), Color::rgb(235, 103, 52)),
MarkerIdentifier::LightSource(..) if hovered => (self.light_source_marker_texture.clone(), Color::rgb(150, 52, 235)),
MarkerIdentifier::LightSource(..) => (self.light_source_marker_texture.clone(), Color::rgb(52, 235, 217)),
MarkerIdentifier::SoundSource(..) if hovered => (self.sound_source_marker_texture.clone(), Color::rgb(128, 52, 235)),
MarkerIdentifier::SoundSource(..) => (self.sound_source_marker_texture.clone(), Color::rgb(235, 52, 140)),
MarkerIdentifier::EffectSource(..) if hovered => (self.effect_source_marker_texture.clone(), Color::rgb(235, 52, 52)),
MarkerIdentifier::EffectSource(..) => (self.effect_source_marker_texture.clone(), Color::rgb(52, 235, 156)),
MarkerIdentifier::Entity(..) if hovered => (self.entity_marker_texture.clone(), Color::rgb(235, 92, 52)),
MarkerIdentifier::Entity(..) => (self.entity_marker_texture.clone(), Color::rgb(189, 235, 52)),
MarkerIdentifier::Object(..) if hovered => (self.object_marker_texture.clone(), Color::rgb_u8(235, 180, 52)),
MarkerIdentifier::Object(..) => (self.object_marker_texture.clone(), Color::rgb_u8(235, 103, 52)),
MarkerIdentifier::LightSource(..) if hovered => (self.light_source_marker_texture.clone(), Color::rgb_u8(150, 52, 235)),
MarkerIdentifier::LightSource(..) => (self.light_source_marker_texture.clone(), Color::rgb_u8(52, 235, 217)),
MarkerIdentifier::SoundSource(..) if hovered => (self.sound_source_marker_texture.clone(), Color::rgb_u8(128, 52, 235)),
MarkerIdentifier::SoundSource(..) => (self.sound_source_marker_texture.clone(), Color::rgb_u8(235, 52, 140)),
MarkerIdentifier::EffectSource(..) if hovered => (self.effect_source_marker_texture.clone(), Color::rgb_u8(235, 52, 52)),
MarkerIdentifier::EffectSource(..) => (self.effect_source_marker_texture.clone(), Color::rgb_u8(52, 235, 156)),
MarkerIdentifier::Entity(..) if hovered => (self.entity_marker_texture.clone(), Color::rgb_u8(235, 92, 52)),
MarkerIdentifier::Entity(..) => (self.entity_marker_texture.clone(), Color::rgb_u8(189, 235, 52)),
_ => panic!(),
};

Expand Down
4 changes: 2 additions & 2 deletions src/interface/cursor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl MouseCursor {
mouse_position - ScreenSize::uniform(15.0 * interface_settings.scaling.get()),
ScreenSize::uniform(30.0 * interface_settings.scaling.get()),
ScreenClip::default(),
Color::monochrome(255),
Color::monochrome_u8(255),
false,
),
Grabbed::Action(sprite, actions, animation_state) => actions.render2(
Expand All @@ -91,7 +91,7 @@ impl MouseCursor {
&animation_state,
mouse_position,
0,
Color::monochrome(255),
Color::monochrome_u8(255),
interface_settings,
),
}
Expand Down
8 changes: 4 additions & 4 deletions src/interface/elements/containers/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl CharacterPreview {
return vec![
Text::default()
.with_text(text.to_owned())
.with_foreground_color(|_| Color::rgb(200, 140, 180))
.with_foreground_color(|_| Color::rgb_u8(200, 140, 180))
.wrap(),
];
}
Expand All @@ -39,13 +39,13 @@ impl CharacterPreview {
return vec![
Text::default()
.with_text(character_information.name.clone())
.with_foreground_color(|_| Color::rgb(220, 210, 210))
.with_foreground_color(|_| Color::rgb_u8(220, 210, 210))
.with_font_size(|_| 18.0)
.wrap(),
Button::default()
.with_text("Switch")
.with_event(UserEvent::RequestSwitchCharacterSlot(slot))
.with_background_color(|_| Color::rgb(161, 141, 141))
.with_background_color(|_| Color::rgb_u8(161, 141, 141))
.with_width(dimension!(50%))
.wrap(),
Button::default()
Expand All @@ -61,7 +61,7 @@ impl CharacterPreview {
vec![
Text::default()
.with_text("New character")
.with_foreground_color(|_| Color::rgb(200, 140, 180))
.with_foreground_color(|_| Color::rgb_u8(200, 140, 180))
.wrap(),
]
}
Expand Down
2 changes: 1 addition & 1 deletion src/interface/elements/containers/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl DialogContainer {
match dialog_element {
DialogElement::Text(text) => Text::default()
.with_text(text.clone())
.with_foreground_color(|_| Color::monochrome(255))
.with_foreground_color(|_| Color::monochrome_u8(255))
.wrap(),
DialogElement::NextButton => Button::default().with_text("next").with_event(UserEvent::NextDialog(npc_id)).wrap(),
DialogElement::CloseButton => Button::default()
Expand Down
2 changes: 1 addition & 1 deletion src/interface/elements/containers/equipment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl EquipmentContainer {

let text = Text::default()
.with_text(slot.display_name().to_string())
.with_foreground_color(|_| Color::monochrome(200))
.with_foreground_color(|_| Color::monochrome_u8(200))
.with_width(dimension!(!))
.wrap();

Expand Down
4 changes: 2 additions & 2 deletions src/interface/elements/containers/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ impl Element for InventoryContainer {

if matches!(mouse_mode, MouseInputMode::MoveItem(..)) {
match self.is_element_self(hovered_element) {
true => renderer.render_background(CornerRadius::uniform(5.0), Color::rgba(60, 160, 160, 160)),
false => renderer.render_background(CornerRadius::uniform(5.0), Color::rgba(160, 160, 60, 160)),
true => renderer.render_background(CornerRadius::uniform(5.0), Color::rgba_u8(60, 160, 160, 160)),
false => renderer.render_background(CornerRadius::uniform(5.0), Color::rgba_u8(160, 160, 60, 160)),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/interface/elements/miscellanious/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Element for Chat {
left: 0.2,
top: offset + 0.2,
},
Color::monochrome(0),
Color::monochrome_u8(0),
theme.chat.font_size.get(),
);

Expand Down
6 changes: 3 additions & 3 deletions src/interface/elements/miscellanious/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ impl Element for ItemBox {

let highlight = (self.highlight)(mouse_mode);
let background_color = match self.is_element_self(hovered_element) || self.is_element_self(focused_element) {
true if highlight => Color::rgba(60, 160, 160, 255),
true if highlight => Color::rgba_u8(60, 160, 160, 255),
true if matches!(mouse_mode, MouseInputMode::None) => theme.button.hovered_background_color.get(),
false if highlight => Color::rgba(160, 160, 60, 255),
false if highlight => Color::rgba_u8(160, 160, 60, 255),
_ => theme.button.background_color.get(),
};

Expand All @@ -88,7 +88,7 @@ impl Element for ItemBox {
item.texture.clone(),
ScreenPosition::default(),
ScreenSize::uniform(30.0),
Color::monochrome(255),
Color::monochrome_u8(255),
);

renderer.render_text(
Expand Down
Loading
Loading