1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
use crate::{
ffi,
math::{Vector3, Vector4},
texture::{get_pixel_data_size, PixelFormat},
};
use static_assertions::{assert_eq_align, assert_eq_size};
/// Color, 4 components, R8G8B8A8 (32bit)
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Color {
/// Color red value
pub r: u8,
/// Color green value
pub g: u8,
/// Color blue value
pub b: u8,
/// Color alpha value
pub a: u8,
}
assert_eq_size!(Color, ffi::Color);
assert_eq_align!(Color, ffi::Color);
impl Color {
/// Light gray color
pub const LIGHTGRAY: Color = Color {
r: 200,
g: 200,
b: 200,
a: 255,
};
/// Gray color
pub const GRAY: Color = Color {
r: 130,
g: 130,
b: 130,
a: 255,
};
/// Dark gray color
pub const DARKGRAY: Color = Color {
r: 80,
g: 80,
b: 80,
a: 255,
};
/// Yellow color
pub const YELLOW: Color = Color {
r: 253,
g: 249,
b: 0,
a: 255,
};
/// Gold color
pub const GOLD: Color = Color {
r: 255,
g: 203,
b: 0,
a: 255,
};
/// Orange color
pub const ORANGE: Color = Color {
r: 255,
g: 161,
b: 0,
a: 255,
};
/// Pink color
pub const PINK: Color = Color {
r: 255,
g: 109,
b: 194,
a: 255,
};
/// Red color
pub const RED: Color = Color {
r: 230,
g: 41,
b: 55,
a: 255,
};
/// Maroon color
pub const MAROON: Color = Color {
r: 190,
g: 33,
b: 55,
a: 255,
};
/// Green color
pub const GREEN: Color = Color {
r: 0,
g: 228,
b: 48,
a: 255,
};
/// Lime color
pub const LIME: Color = Color {
r: 0,
g: 158,
b: 47,
a: 255,
};
/// Dark green color
pub const DARKGREEN: Color = Color {
r: 0,
g: 117,
b: 44,
a: 255,
};
/// Sky blue color
pub const SKYBLUE: Color = Color {
r: 102,
g: 191,
b: 255,
a: 255,
};
/// Blue color
pub const BLUE: Color = Color {
r: 0,
g: 121,
b: 241,
a: 255,
};
/// Dark blue color
pub const DARKBLUE: Color = Color {
r: 0,
g: 82,
b: 172,
a: 255,
};
/// Purple color
pub const PURPLE: Color = Color {
r: 200,
g: 122,
b: 255,
a: 255,
};
/// Violet color
pub const VIOLET: Color = Color {
r: 135,
g: 60,
b: 190,
a: 255,
};
/// Dark purple color
pub const DARKPURPLE: Color = Color {
r: 112,
g: 31,
b: 126,
a: 255,
};
/// Beige color
pub const BEIGE: Color = Color {
r: 211,
g: 176,
b: 131,
a: 255,
};
/// Brown color
pub const BROWN: Color = Color {
r: 127,
g: 106,
b: 79,
a: 255,
};
/// Dark brown color
pub const DARKBROWN: Color = Color {
r: 76,
g: 63,
b: 47,
a: 255,
};
/// White color
pub const WHITE: Color = Color {
r: 255,
g: 255,
b: 255,
a: 255,
};
/// Black color
pub const BLACK: Color = Color {
r: 0,
g: 0,
b: 0,
a: 255,
};
/// Blank color (alpha = 0)
pub const BLANK: Color = Color {
r: 0,
g: 0,
b: 0,
a: 0,
};
/// Magenta color
pub const MAGENTA: Color = Color {
r: 255,
g: 0,
b: 255,
a: 255,
};
/// Raylib white color
pub const RAYWHITE: Color = Color {
r: 245,
g: 245,
b: 245,
a: 255,
};
/// Convenience function for creating a new color
#[inline]
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
/// Get color with alpha applied, alpha goes from 0.0f to 1.0f
#[inline]
pub fn fade(self, alpha: f32) -> Self {
unsafe { ffi::Fade(self.into(), alpha).into() }
}
/// Get hexadecimal value for a Color
#[inline]
pub fn to_hex(self) -> u32 {
// no real need to use ffi here
((self.r as u32) << 24) | ((self.g as u32) << 16) | ((self.b as u32) << 8) | (self.a as u32)
}
/// Get Color structure from hexadecimal value
#[inline]
pub fn from_hex(val: u32) -> Self {
// no real need to use ffi here
Self {
r: (val >> 24 & 0xFF) as u8,
g: (val >> 16 & 0xFF) as u8,
b: (val >> 8 & 0xFF) as u8,
a: (val & 0xFF) as u8,
}
}
/// Get Color normalized as float [0..1]
#[inline]
pub fn normalize(self) -> Vector4 {
// no real need to use ffi here
Vector4 {
x: self.r as f32 / 255.,
y: self.g as f32 / 255.,
z: self.b as f32 / 255.,
w: self.a as f32 / 255.,
}
}
/// Get Color from normalized values [0..1]
#[inline]
pub fn from_normalized(normalized: Vector4) -> Self {
// no real need to use ffi here
Self {
r: (normalized.x * 255.) as u8,
g: (normalized.y * 255.) as u8,
b: (normalized.z * 255.) as u8,
a: (normalized.w * 255.) as u8,
}
}
/// Get HSV values for a Color, hue [0..360], saturation/value [0..1]
#[inline]
pub fn to_hsv(self) -> Vector3 {
unsafe { ffi::ColorToHSV(self.into()).into() }
}
/// Get a Color from HSV values, hue [0..360], saturation/value [0..1]
#[inline]
pub fn from_hsv(hue: f32, saturation: f32, value: f32) -> Self {
unsafe { ffi::ColorFromHSV(hue, saturation, value).into() }
}
/// Get color multiplied with another color
#[inline]
pub fn tint(self, tint: Self) -> Self {
unsafe { ffi::ColorTint(self.into(), tint.into()).into() }
}
/// Get color with brightness correction, brightness factor goes from -1.0f to 1.0f
#[inline]
pub fn brightness(self, factor: f32) -> Self {
unsafe { ffi::ColorBrightness(self.into(), factor).into() }
}
/// Get color with contrast correction, contrast values between -1.0f and 1.0f
#[inline]
pub fn contrast(self, contrast: f32) -> Self {
unsafe { ffi::ColorContrast(self.into(), contrast).into() }
}
/// Get color with alpha applied, alpha goes from 0.0f to 1.0f
#[inline]
pub fn alpha(self, alpha: f32) -> Self {
unsafe { ffi::ColorAlpha(self.into(), alpha).into() }
}
/// Get src alpha-blended into dst color with tint
#[inline]
pub fn alpha_blend(self, src: Self, tint: Self) -> Self {
unsafe { ffi::ColorAlphaBlend(self.into(), src.into(), tint.into()).into() }
}
/// Get Color from a source pixel pointer of certain format (uncompressed formats only)
///
/// Returns `None` if buffer isn't large enough
#[inline]
pub fn get_pixel_color(source: &[u8], format: PixelFormat) -> Option<Self> {
if source.len() >= get_pixel_data_size(1, 1, format) {
unsafe {
Some(
ffi::GetPixelColor(source.as_ptr() as *mut core::ffi::c_void, format as _)
.into(),
)
}
} else {
None
}
}
/// Set color formatted into destination pixel pointer (uncompressed formats only)
///
/// Returns `true` on success, `false` if buffer isn't large enough
#[inline]
pub fn set_pixel_color(self, dest: &mut [u8], format: PixelFormat) -> bool {
if dest.len() >= get_pixel_data_size(1, 1, format) {
unsafe {
ffi::SetPixelColor(
dest.as_mut_ptr() as *mut core::ffi::c_void,
self.into(),
format as _,
);
}
true
} else {
false
}
}
}
impl From<Color> for ffi::Color {
#[inline]
fn from(val: Color) -> Self {
unsafe { std::mem::transmute(val) }
}
}
impl From<ffi::Color> for Color {
#[inline]
fn from(value: ffi::Color) -> Self {
unsafe { std::mem::transmute(value) }
}
}