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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
// Copyright (c) 2023 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Lesser General Public License that can be found
// in the LICENSE file.
use bitflags::bitflags;
use std::marker::PhantomData;
use std::ops::{Index, IndexMut, Mul};
use crate::core::alpha_type::AlphaType;
use crate::core::scalar::{Scalar, ScalarExt};
use crate::core::types::{A32_SHIFT, B32_SHIFT, G32_SHIFT, R32_SHIFT};
/// 8-bit type for an alpha value. 255 is 100% opaque, zero is 100% transparent.
pub type Alpha = u8;
/// Represents fully transparent Alpha value. Alpha ranges from zero,
/// fully transparent; to 255, fully opaque.
pub const ALPHA_TRANSPARENT: Alpha = 0x00;
/// Represents fully opaque Alpha value. Alpha ranges from zero,
/// fully transparent; to 255, fully opaque.
pub const ALPHA_OPAQUE: Alpha = 0xFF;
/// 32-bit ARGB color value, unpremultiplied.
///
/// Color components are always in a known order. This is different from `PMColor`,
/// which has its bytes in a configuration dependent order, to match
/// the format of Bgra8888 bitmaps. Color is the type used to specify colors
/// in Paint and in gradients.
///
/// Color that is premultiplied has the same component values as color
/// that is unpremultiplied if alpha is 255, fully opaque, although may have the
/// component values in a different order.
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Color {
alpha: u8,
red: u8,
green: u8,
blue: u8,
}
impl Color {
/// Returns color value from 8-bit component values.
///
/// Since color is unpremultiplied, `alpha` may be smaller than the largest of `red`, `green`, and `blue`.
///
/// # Arguments
/// - `alpha` - amount of alpha, from fully transparent (0) to fully opaque (255)
/// - `red` - amount of red, from no red (0) to full red (255)
/// - `green` - amount of green, from no green (0) to full green (255)
/// - `blue` - amount of blue, from no blue (0) to full blue (255)
#[must_use]
pub const fn from_argb(alpha: u8, red: u8, green: u8, blue: u8) -> Self {
Self {
alpha,
red,
green,
blue,
}
}
/// Returns color value from 8-bit component values, with alpha set fully opaque to 255.
#[must_use]
pub const fn from_rgb(red: u8, green: u8, blue: u8) -> Self {
Self::from_argb(0xFF, red, green, blue)
}
/// Returns alpha byte from color value.
#[must_use]
pub const fn alpha(self) -> u8 {
self.alpha
}
/// Returns red component of color, from zero to 255.
#[must_use]
pub const fn red(self) -> u8 {
self.red
}
/// Returns green component of color, from zero to 255.
#[must_use]
pub const fn green(self) -> u8 {
self.green
}
/// Returns blue component of color, from zero to 255.
#[must_use]
pub const fn blue(self) -> u8 {
self.blue
}
/// Returns unpremultiplied color with red, blue, and green set from self; and alpha set
/// from `alpha`.
///
/// Alpha component of self is ignored and is replaced by `alpha` in result.
#[must_use]
pub const fn set_alpha(self, alpha: u8) -> Self {
Self { alpha, ..self }
}
}
/// Represents fully transparent Color. May be used to initialize a destination
/// containing a mask or a non-rectangular image.
pub const COLOR_TRANSPARENT: Color = Color::from_argb(0x00, 0x00, 0x00, 0x00);
/// Represents fully opaque black.
pub const COLOR_BLACK: Color = Color::from_argb(0xFF, 0x00, 0x00, 0x00);
/// Represents fully opaque dark gray.
///
/// Note that SVG dark gray is equivalent to 0xFFA9A9A9.
pub const COLOR_DKGRAY: Color = Color::from_argb(0xFF, 0x44, 0x44, 0x44);
/// Represents fully opaque gray.
///
/// Note that HTML gray is equivalent to 0xFF808080.
pub const COLOR_GRAY: Color = Color::from_argb(0xFF, 0x88, 0x88, 0x88);
/// Represents fully opaque light gray. HTML silver is equivalent to 0xFFC0C0C0.
///
/// Note that SVG light gray is equivalent to 0xFFD3D3D3.
pub const COLOR_LTGRAY: Color = Color::from_argb(0xFF, 0xCC, 0xCC, 0xCC);
/// Represents fully opaque white.
pub const COLOR_WHITE: Color = Color::from_argb(0xFF, 0xFF, 0xFF, 0xFF);
/// Represents fully opaque red.
pub const COLOR_RED: Color = Color::from_argb(0xFF, 0xFF, 0x00, 0x00);
/// Represents fully opaque green. HTML lime is equivalent.
///
/// Note that HTML green is equivalent to 0xFF008000.
pub const COLOR_GREEN: Color = Color::from_argb(0xFF, 0x00, 0xFF, 0x00);
/// Represents fully opaque blue.
pub const COLOR_BLUE: Color = Color::from_argb(0xFF, 0x00, 0x00, 0xFF);
/// Represents fully opaque yellow.
pub const COLOR_YELLOW: Color = Color::from_argb(0xFF, 0xFF, 0xFF, 0x00);
/// Represents fully opaque cyan. HTML aqua is equivalent.
pub const COLOR_CYAN: Color = Color::from_argb(0xFF, 0x00, 0xFF, 0xFF);
/// Represents fully opaque magenta. HTML fuchsia is equivalent.
pub const COLOR_MAGENTA: Color = Color::from_argb(0xFF, 0xFF, 0x00, 0xFF);
pub struct Hsv {
/// From zero to less than 360
pub hue: Scalar,
/// From zero to one.
pub saturation: Scalar,
/// From zero to one.
pub value: Scalar,
}
/// Converts RGB to its HSV components.
///
/// Alpha value is dropped.
impl From<Color> for Hsv {
fn from(_color: Color) -> Self {
unimplemented!()
}
}
/// Converts HSV components to an ARGB color.
///
/// Alpha is set to 255.
impl From<&Hsv> for Color {
fn from(hsv: &Hsv) -> Self {
hsv.to_color(0xFF)
}
}
impl Hsv {
/// Converts HSV components to an ARGB color.
///
/// Alpha is passed through unchanged.
#[must_use]
pub fn to_color(&self, _alpha: Alpha) -> Color {
unimplemented!()
}
}
/// 32-bit ARGB color value, premultiplied.
///
/// The byte order for this value is configuration dependent, matching
/// the format of BGRA8888 bitmaps. This is different from Color, which is unpremultiplied,
/// and is always in the same byte order.
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PMColor {
/// Amount of alpha, from fully transparent (0) to fully opaque (255)
alpha: u8,
/// Amount of red, from no red (0) to full red (255)
red: u8,
/// Amount of green, from no green (0) to full green (255)
green: u8,
/// amount of blue, from no blue (0) to full blue (255)
blue: u8,
}
/// Returns pmcolor closest to Color `color`.
///
/// Multiplies `color` RGB components by the `color` alpha,
/// and arranges the bytes to match the format of `ColorType` `N32`.
impl From<Color> for PMColor {
fn from(_color: Color) -> Self {
unimplemented!()
}
}
impl From<PMColor> for u32 {
#[allow(clippy::use_self)]
fn from(color: PMColor) -> u32 {
(u32::from(color.alpha) << A32_SHIFT)
| (u32::from(color.red) << R32_SHIFT)
| (u32::from(color.green) << G32_SHIFT)
| (u32::from(color.blue) << B32_SHIFT)
}
}
impl From<u32> for PMColor {
fn from(packed: u32) -> Self {
let alpha = ((packed << (24 - A32_SHIFT)) >> 24) as u8;
let red = ((packed << (24 - R32_SHIFT)) >> 24) as u8;
let green = ((packed << (24 - G32_SHIFT)) >> 24) as u8;
let blue = ((packed << (24 - B32_SHIFT)) >> 24) as u8;
Self::from_argb(alpha, red, green, blue)
}
}
impl PMColor {
#[must_use]
pub const fn from_argb(alpha: u8, red: u8, green: u8, blue: u8) -> Self {
Self {
alpha,
red,
green,
blue,
}
}
#[must_use]
pub const fn alpha(&self) -> u8 {
self.alpha
}
#[must_use]
pub const fn red(&self) -> u8 {
self.red
}
#[must_use]
pub const fn green(&self) -> u8 {
self.green
}
#[must_use]
pub const fn blue(&self) -> u8 {
self.blue
}
}
/// `ColorChannel` describes different color channels one can manipulate
pub enum ColorChannel {
/// the red channel
Red,
/// the green channel
Green,
/// the blue channel
Blue,
/// the alpha channel
Alpha,
}
bitflags! {
/// Used to represent the channels available in a color type or texture format as a mask.
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ColorChannelFlag: u32 {
const Red = 1 << ColorChannel::Red as u8;
const Green = 1 << ColorChannel::Green as u8;
const Blue = 1 << ColorChannel::Blue as u8;
const Alpha = 1 << ColorChannel::Alpha as u8;
const Gray = 0x10;
// Convenience values
const GrayAlpha = Self::Gray.bits() | Self::Alpha.bits();
const RG = Self::Red.bits() | Self::Green.bits();
const RGB = Self::RG.bits() | Self::Blue.bits();
const RGBA = Self::RGB.bits() | Self::Alpha.bits();
}
}
/// `Rgba4F` represents RGBA color value, holding four floating point components.
///
/// Color components are always in a known order.
/// `AlphaType` determines if the `Rgba4F`'s R, G, and B components are premultiplied by alpha or not.
///
/// Crate public API always uses unpremultiplied colors, which can be stored as
/// `Rgba4F<Unpremul>`. For convenience, this type can also be referred to as `Color4f`.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Rgba4F<AlphaType> {
red: f32,
green: f32,
blue: f32,
alpha: f32,
phantom: PhantomData<AlphaType>,
}
/// Returns `Rgba4F` multiplied by scale.
impl Mul<f32> for Rgba4F<AlphaType> {
type Output = Self;
fn mul(self, scale: f32) -> Self {
Self {
red: self.red * scale,
green: self.green * scale,
blue: self.blue * scale,
alpha: self.alpha * scale,
phantom: self.phantom,
}
}
}
/// Returns `Rgba4F` multiplied component-wise by scale.
impl Mul<&Self> for Rgba4F<AlphaType> {
type Output = Self;
fn mul(self, other: &Self) -> Self {
Self {
red: self.red * other.red,
green: self.green * other.green,
blue: self.blue * other.blue,
alpha: self.alpha * other.alpha,
phantom: self.phantom,
}
}
}
/// Returns one component.
///
/// Index should not be larger than 3.
impl Index<usize> for Rgba4F<AlphaType> {
type Output = f32;
fn index(&self, index: usize) -> &Self::Output {
debug_assert!(index < 4);
match index {
0 => &self.red,
1 => &self.green,
2 => &self.blue,
3 => &self.alpha,
_ => panic!("Index out of range"),
}
}
}
impl IndexMut<usize> for Rgba4F<AlphaType> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
debug_assert!(index < 4);
match index {
0 => &mut self.red,
1 => &mut self.green,
2 => &mut self.blue,
3 => &mut self.alpha,
_ => panic!("Index out of range"),
}
}
}
impl Rgba4F<AlphaType> {
#[must_use]
pub const fn from_rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self {
red,
green,
blue,
alpha,
phantom: PhantomData::<AlphaType>,
}
}
/// Returns a pointer to components of `Rgba4F`, for array access.
///
/// Orders in array is [red, green, blue, alpha]
#[must_use]
pub const fn to_vec(&self) -> [f32; 4] {
[self.red, self.green, self.blue, self.alpha]
}
/// Returns true if `Rgba4F` is an opaque color.
#[must_use]
pub fn is_opaque(&self) -> bool {
debug_assert!((0.0..=1.0).contains(&self.alpha));
self.alpha.fuzzy_equal(1.0)
}
/// Returns true if all channels are in [0, 1].
#[must_use]
pub fn fits_in_bytes(&self) -> bool {
let range = 0.0..=1.0;
debug_assert!(range.contains(&self.alpha));
range.contains(&self.red) && range.contains(&self.green) && range.contains(&self.blue)
}
/// Returns a copy of the `Rgba4F` but with alpha component set to 1.0f.
#[must_use]
pub const fn new_opaque(&self) -> Self {
Self {
red: self.red,
green: self.green,
blue: self.blue,
alpha: 1.0,
phantom: self.phantom,
}
}
}
// TODO(Shaohua): Replace with partial specialization of Rgba4F<AlphaType>
/// `Color4f` represents RGBA color value, holding four floating point components.
///
/// Color components are always in a known order, and are unpremultiplied.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Color4f {
red: f32,
green: f32,
blue: f32,
alpha: f32,
}
/// Returns `Color4f` multiplied by scale.
impl Mul<f32> for Color4f {
type Output = Self;
fn mul(self, scale: f32) -> Self {
Self {
red: self.red * scale,
green: self.green * scale,
blue: self.blue * scale,
alpha: self.alpha * scale,
}
}
}
/// Returns `Color4f` multiplied component-wise by scale.
impl Mul<&Self> for Color4f {
type Output = Self;
fn mul(self, other: &Self) -> Self {
Self {
red: self.red * other.red,
green: self.green * other.green,
blue: self.blue * other.blue,
alpha: self.alpha * other.alpha,
}
}
}
/// Returns one component.
///
/// Index should not be larger than 3.
impl Index<usize> for Color4f {
type Output = f32;
fn index(&self, index: usize) -> &Self::Output {
debug_assert!(index < 4);
match index {
0 => &self.red,
1 => &self.green,
2 => &self.blue,
3 => &self.alpha,
_ => panic!("Index out of range"),
}
}
}
impl IndexMut<usize> for Color4f {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
debug_assert!(index < 4);
match index {
0 => &mut self.red,
1 => &mut self.green,
2 => &mut self.blue,
3 => &mut self.alpha,
_ => panic!("Index out of range"),
}
}
}
impl From<Color> for Color4f {
fn from(_color: Color) -> Self {
unimplemented!()
}
}
impl From<&Color4f> for Color {
fn from(_color: &Color4f) -> Self {
unimplemented!()
}
}
impl From<Color4f> for Color {
fn from(_color: Color4f) -> Self {
unimplemented!()
}
}
impl Color4f {
#[must_use]
pub const fn from_rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self {
red,
green,
blue,
alpha,
}
}
#[must_use]
pub const fn red(&self) -> f32 {
self.red
}
#[must_use]
pub const fn green(&self) -> f32 {
self.green
}
#[must_use]
pub const fn blue(&self) -> f32 {
self.blue
}
#[must_use]
pub const fn alpha(&self) -> f32 {
self.alpha
}
pub fn set_alpha(&mut self, alpha: f32) {
debug_assert!((0.0..=1.0).contains(&alpha));
self.alpha = alpha;
}
/// Returns a pointer to components of `Color4f`, for array access.
///
/// Orders in array is [red, green, blue, alpha]
#[must_use]
pub const fn to_vec(&self) -> [f32; 4] {
[self.red, self.green, self.blue, self.alpha]
}
/// Returns true if `Color4f` is an opaque color.
#[must_use]
pub fn is_opaque(&self) -> bool {
debug_assert!((0.0..=1.0).contains(&self.alpha));
self.alpha.fuzzy_equal(1.0)
}
/// Returns true if all channels are in [0, 1].
#[must_use]
pub fn fits_in_bytes(&self) -> bool {
let range = 0.0..=1.0;
debug_assert!(range.contains(&self.alpha));
range.contains(&self.red) && range.contains(&self.green) && range.contains(&self.blue)
}
/// Returns a copy of the `Color4f` but with alpha component set to 1.0f.
#[must_use]
pub const fn new_opaque(&self) -> Self {
Self {
red: self.red,
green: self.green,
blue: self.blue,
alpha: 1.0,
}
}
#[must_use]
pub const fn to_bytes_rgba(&self) -> u32 {
unimplemented!()
}
#[must_use]
pub fn from_bytes_rgba(_color: u32) -> Self {
unimplemented!()
}
}
pub mod colors {
use super::Color4f;
pub const TRANSPARENT: Color4f = Color4f::from_rgba(0.0, 0.0, 0.0, 0.0);
pub const BLACK: Color4f = Color4f::from_rgba(0.0, 0.0, 0.0, 1.0);
pub const DARK_GRAY: Color4f = Color4f::from_rgba(0.25, 0.25, 0.25, 1.0);
pub const GRAY: Color4f = Color4f::from_rgba(0.50, 0.50, 0.50, 1.0);
pub const LIGHT_GRAY: Color4f = Color4f::from_rgba(0.75, 0.75, 0.75, 1.0);
pub const WHITE: Color4f = Color4f::from_rgba(1.0, 1.0, 1.0, 1.0);
pub const RED: Color4f = Color4f::from_rgba(1.0, 0.0, 0.0, 1.0);
pub const GREEN: Color4f = Color4f::from_rgba(0.0, 1.0, 0.0, 1.0);
pub const BLUE: Color4f = Color4f::from_rgba(0.0, 0.0, 1.0, 1.0);
pub const YELLOW: Color4f = Color4f::from_rgba(1.0, 1.0, 0.0, 1.0);
pub const CYAN: Color4f = Color4f::from_rgba(0.0, 1.0, 1.0, 1.0);
pub const MAGENTA: Color4f = Color4f::from_rgba(1.0, 0.0, 1.0, 1.0);
}