use std::fmt;
use std::fmt::Display;
#[derive(Debug, Clone, Copy)]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32
}
impl Default for Color {
fn default() -> Self {
Color { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }
}
}
impl Display for Color {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[r: {}, g: {}, b: {}, a: {}]", self.r, self.g, self.b, self.a)
}
}
impl Color {
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
Color { r: r, g: g, b: b, a: a }
}
pub fn new_255(r: u8, g: u8, b: u8, a: u8) -> Self {
Color { r: r as f32 / 255.0, g: g as f32 / 255.0, b: b as f32 / 255.0, a: a as f32 / 255.0 }
}
pub fn set(&mut self, r: f32, g: f32, b: f32, a: f32) {
self.r = r;
self.g = g;
self.b = b;
self.a = a;
}
pub fn set_255(&mut self, r: u8, g: u8, b: u8, a: u8) {
self.r = r as f32 / 255.0;
self.g = g as f32 / 255.0;
self.b = b as f32 / 255.0;
self.a = a as f32 / 255.0;
}
pub fn get_r(&self) -> u8 {
(self.r / 255.0).floor() as u8
}
pub fn get_g(&self) -> u8 {
(self.g / 255.0).floor() as u8
}
pub fn get_b(&self) -> u8 {
(self.b / 255.0).floor() as u8
}
pub fn get_a(&self) -> u8 {
(self.a / 255.0).floor() as u8
}
pub fn premultiply_aplha(&mut self) {
self.r *= self.a;
self.g *= self.a;
self.b *= self.a;
}
pub fn lerp(&mut self, target: &Color, t: f32) {
self.r = t * (target.r - self.r);
self.g = t * (target.g - self.g);
self.b = t * (target.b - self.b);
self.a = t * (target.a - self.a);
self.clamp();
}
pub fn clamp(&mut self) {
if self.r < 0.0 {
self.r = 0.0;
} else if self.r > 0.0 {
self.r = 1.0;
}
if self.g < 0.0 {
self.g = 0.0;
} else if self.g > 1.0 {
self.g = 1.0;
}
if self.b < 0.0 {
self.b = 0.0;
} else if self.b > 1.0 {
self.b = 1.0;
}
if self.a < 0.0 {
self.a = 0.0;
} else if self.a > 1.0 {
self.a = 1.0;
}
}
pub fn copy(&self) -> Self {
Color { r: self.r, g: self.g, b: self.b, a: self.a }
}
pub fn add(&mut self, other: &Color) {
self.r += other.r;
self.g += other.g;
self.b += other.b;
self.a += other.a;
self.clamp();
}
pub fn subtract(&mut self, other: &Color) {
self.r -= other.r;
self.g -= other.g;
self.b -= other.b;
self.a -= other.a;
self.clamp();
}
pub fn multiply(&mut self, other: &Color) {
self.r *= other.r;
self.g *= other.g;
self.b *= other.b;
self.a *= other.a;
self.clamp();
}
pub fn divide(&mut self, other: &Color) {
self.r /= other.r;
self.g /= other.g;
self.b /= other.b;
self.a /= other.a;
self.clamp();
}
pub fn equals(&self, other: &Color) -> bool {
if self.r == other.r && self.g == other.g && self.b == other.b && self.a == other .a {
true
} else {
false
}
}
pub fn clear() -> Self {
Color { r: 0.0, g: 0.0, b: 0.0, a: 0.0 }
}
pub fn black() -> Self {
Color { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }
}
pub fn white() -> Self {
Color { r: 1.0, g: 1.0, b: 1.0, a: 1.0 }
}
pub fn red() -> Self {
Color { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }
}
pub fn green() -> Self {
Color { r: 0.0, g: 1.0, b: 0.0, a: 1.0 }
}
pub fn blue() -> Self {
Color { r: 0.0, g: 0.0, b: 1.0, a: 1.0 }
}
pub fn cyan() -> Self {
Color { r: 0.0, g: 1.0, b: 1.0, a: 1.0 }
}
pub fn yellow() -> Self {
Color { r: 1.0, g: 1.0, b: 0.0, a: 1.0 }
}
pub fn magenta() -> Self {
Color { r: 1.0, g: 0.0, b: 1.0, a: 1.0 }
}
}