Remove 2 suffix from gpui
Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
parent
3c81dda8e2
commit
f5ba22659b
225 changed files with 8511 additions and 41063 deletions
|
@ -1,65 +1,223 @@
|
|||
use std::{
|
||||
borrow::Cow,
|
||||
fmt,
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
use anyhow::bail;
|
||||
use serde::de::{self, Deserialize, Deserializer, Visitor};
|
||||
use std::fmt;
|
||||
|
||||
use crate::json::ToJson;
|
||||
use pathfinder_color::{ColorF, ColorU};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{
|
||||
de::{self, Unexpected},
|
||||
Deserialize, Deserializer,
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord, JsonSchema)]
|
||||
#[repr(transparent)]
|
||||
pub struct Color(#[schemars(with = "String")] pub ColorU);
|
||||
|
||||
pub fn color(rgba: u32) -> Color {
|
||||
Color::from_u32(rgba)
|
||||
pub fn rgb<C: From<Rgba>>(hex: u32) -> C {
|
||||
let r = ((hex >> 16) & 0xFF) as f32 / 255.0;
|
||||
let g = ((hex >> 8) & 0xFF) as f32 / 255.0;
|
||||
let b = (hex & 0xFF) as f32 / 255.0;
|
||||
Rgba { r, g, b, a: 1.0 }.into()
|
||||
}
|
||||
|
||||
pub fn rgb(r: f32, g: f32, b: f32) -> Color {
|
||||
Color(ColorF::new(r, g, b, 1.).to_u8())
|
||||
pub fn rgba(hex: u32) -> Rgba {
|
||||
let r = ((hex >> 24) & 0xFF) as f32 / 255.0;
|
||||
let g = ((hex >> 16) & 0xFF) as f32 / 255.0;
|
||||
let b = ((hex >> 8) & 0xFF) as f32 / 255.0;
|
||||
let a = (hex & 0xFF) as f32 / 255.0;
|
||||
Rgba { r, g, b, a }
|
||||
}
|
||||
|
||||
pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Color {
|
||||
Color(ColorF::new(r, g, b, a).to_u8())
|
||||
#[derive(PartialEq, Clone, Copy, Default)]
|
||||
pub struct Rgba {
|
||||
pub r: f32,
|
||||
pub g: f32,
|
||||
pub b: f32,
|
||||
pub a: f32,
|
||||
}
|
||||
|
||||
pub fn transparent_black() -> Color {
|
||||
Color(ColorU::transparent_black())
|
||||
impl fmt::Debug for Rgba {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "rgba({:#010x})", u32::from(*self))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn black() -> Color {
|
||||
Color(ColorU::black())
|
||||
impl Rgba {
|
||||
pub fn blend(&self, other: Rgba) -> Self {
|
||||
if other.a >= 1.0 {
|
||||
other
|
||||
} else if other.a <= 0.0 {
|
||||
return *self;
|
||||
} else {
|
||||
return Rgba {
|
||||
r: (self.r * (1.0 - other.a)) + (other.r * other.a),
|
||||
g: (self.g * (1.0 - other.a)) + (other.g * other.a),
|
||||
b: (self.b * (1.0 - other.a)) + (other.b * other.a),
|
||||
a: self.a,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn white() -> Color {
|
||||
Color(ColorU::white())
|
||||
impl From<Rgba> for u32 {
|
||||
fn from(rgba: Rgba) -> Self {
|
||||
let r = (rgba.r * 255.0) as u32;
|
||||
let g = (rgba.g * 255.0) as u32;
|
||||
let b = (rgba.b * 255.0) as u32;
|
||||
let a = (rgba.a * 255.0) as u32;
|
||||
(r << 24) | (g << 16) | (b << 8) | a
|
||||
}
|
||||
}
|
||||
|
||||
pub fn red() -> Color {
|
||||
color(0xff0000ff)
|
||||
struct RgbaVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for RgbaVisitor {
|
||||
type Value = Rgba;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a string in the format #rrggbb or #rrggbbaa")
|
||||
}
|
||||
|
||||
fn visit_str<E: de::Error>(self, value: &str) -> Result<Rgba, E> {
|
||||
Rgba::try_from(value).map_err(E::custom)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn green() -> Color {
|
||||
color(0x00ff00ff)
|
||||
impl<'de> Deserialize<'de> for Rgba {
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
deserializer.deserialize_str(RgbaVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blue() -> Color {
|
||||
color(0x0000ffff)
|
||||
impl From<Hsla> for Rgba {
|
||||
fn from(color: Hsla) -> Self {
|
||||
let h = color.h;
|
||||
let s = color.s;
|
||||
let l = color.l;
|
||||
|
||||
let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
|
||||
let x = c * (1.0 - ((h * 6.0) % 2.0 - 1.0).abs());
|
||||
let m = l - c / 2.0;
|
||||
let cm = c + m;
|
||||
let xm = x + m;
|
||||
|
||||
let (r, g, b) = match (h * 6.0).floor() as i32 {
|
||||
0 | 6 => (cm, xm, m),
|
||||
1 => (xm, cm, m),
|
||||
2 => (m, cm, xm),
|
||||
3 => (m, xm, cm),
|
||||
4 => (xm, m, cm),
|
||||
_ => (cm, m, xm),
|
||||
};
|
||||
|
||||
Rgba {
|
||||
r,
|
||||
g,
|
||||
b,
|
||||
a: color.a,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn yellow() -> Color {
|
||||
color(0xffff00ff)
|
||||
impl TryFrom<&'_ str> for Rgba {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(value: &'_ str) -> Result<Self, Self::Error> {
|
||||
const RGB: usize = "rgb".len();
|
||||
const RGBA: usize = "rgba".len();
|
||||
const RRGGBB: usize = "rrggbb".len();
|
||||
const RRGGBBAA: usize = "rrggbbaa".len();
|
||||
|
||||
const EXPECTED_FORMATS: &str = "Expected #rgb, #rgba, #rrggbb, or #rrggbbaa";
|
||||
|
||||
let Some(("", hex)) = value.trim().split_once('#') else {
|
||||
bail!("invalid RGBA hex color: '{value}'. {EXPECTED_FORMATS}");
|
||||
};
|
||||
|
||||
let (r, g, b, a) = match hex.len() {
|
||||
RGB | RGBA => {
|
||||
let r = u8::from_str_radix(&hex[0..1], 16)?;
|
||||
let g = u8::from_str_radix(&hex[1..2], 16)?;
|
||||
let b = u8::from_str_radix(&hex[2..3], 16)?;
|
||||
let a = if hex.len() == RGBA {
|
||||
u8::from_str_radix(&hex[3..4], 16)?
|
||||
} else {
|
||||
0xf
|
||||
};
|
||||
|
||||
/// Duplicates a given hex digit.
|
||||
/// E.g., `0xf` -> `0xff`.
|
||||
const fn duplicate(value: u8) -> u8 {
|
||||
value << 4 | value
|
||||
}
|
||||
|
||||
(duplicate(r), duplicate(g), duplicate(b), duplicate(a))
|
||||
}
|
||||
RRGGBB | RRGGBBAA => {
|
||||
let r = u8::from_str_radix(&hex[0..2], 16)?;
|
||||
let g = u8::from_str_radix(&hex[2..4], 16)?;
|
||||
let b = u8::from_str_radix(&hex[4..6], 16)?;
|
||||
let a = if hex.len() == RRGGBBAA {
|
||||
u8::from_str_radix(&hex[6..8], 16)?
|
||||
} else {
|
||||
0xff
|
||||
};
|
||||
(r, g, b, a)
|
||||
}
|
||||
_ => bail!("invalid RGBA hex color: '{value}'. {EXPECTED_FORMATS}"),
|
||||
};
|
||||
|
||||
Ok(Rgba {
|
||||
r: r as f32 / 255.,
|
||||
g: g as f32 / 255.,
|
||||
b: b as f32 / 255.,
|
||||
a: a as f32 / 255.,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Color {
|
||||
pub fn transparent_black() -> Self {
|
||||
transparent_black()
|
||||
#[derive(Default, Copy, Clone, Debug)]
|
||||
#[repr(C)]
|
||||
pub struct Hsla {
|
||||
pub h: f32,
|
||||
pub s: f32,
|
||||
pub l: f32,
|
||||
pub a: f32,
|
||||
}
|
||||
|
||||
impl PartialEq for Hsla {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.h
|
||||
.total_cmp(&other.h)
|
||||
.then(self.s.total_cmp(&other.s))
|
||||
.then(self.l.total_cmp(&other.l).then(self.a.total_cmp(&other.a)))
|
||||
.is_eq()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Hsla {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
// SAFETY: The total ordering relies on this always being Some()
|
||||
Some(
|
||||
self.h
|
||||
.total_cmp(&other.h)
|
||||
.then(self.s.total_cmp(&other.s))
|
||||
.then(self.l.total_cmp(&other.l).then(self.a.total_cmp(&other.a))),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Hsla {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
// SAFETY: The partial comparison is a total comparison
|
||||
unsafe { self.partial_cmp(other).unwrap_unchecked() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Hsla {
|
||||
pub fn to_rgb(self) -> Rgba {
|
||||
self.into()
|
||||
}
|
||||
|
||||
pub fn red() -> Self {
|
||||
red()
|
||||
}
|
||||
|
||||
pub fn green() -> Self {
|
||||
green()
|
||||
}
|
||||
|
||||
pub fn blue() -> Self {
|
||||
blue()
|
||||
}
|
||||
|
||||
pub fn black() -> Self {
|
||||
|
@ -70,107 +228,230 @@ impl Color {
|
|||
white()
|
||||
}
|
||||
|
||||
pub fn red() -> Self {
|
||||
Color::from_u32(0xff0000ff)
|
||||
}
|
||||
|
||||
pub fn green() -> Self {
|
||||
Color::from_u32(0x00ff00ff)
|
||||
}
|
||||
|
||||
pub fn blue() -> Self {
|
||||
Color::from_u32(0x0000ffff)
|
||||
}
|
||||
|
||||
pub fn yellow() -> Self {
|
||||
Color::from_u32(0xffff00ff)
|
||||
}
|
||||
|
||||
pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
|
||||
Self(ColorU::new(r, g, b, a))
|
||||
}
|
||||
|
||||
pub fn from_u32(rgba: u32) -> Self {
|
||||
Self(ColorU::from_u32(rgba))
|
||||
}
|
||||
|
||||
pub fn blend(source: Color, dest: Color) -> Color {
|
||||
// Skip blending if we don't need it.
|
||||
if source.a == 255 {
|
||||
return source;
|
||||
} else if source.a == 0 {
|
||||
return dest;
|
||||
}
|
||||
|
||||
let source = source.0.to_f32();
|
||||
let dest = dest.0.to_f32();
|
||||
|
||||
let a = source.a() + (dest.a() * (1. - source.a()));
|
||||
let r = ((source.r() * source.a()) + (dest.r() * dest.a() * (1. - source.a()))) / a;
|
||||
let g = ((source.g() * source.a()) + (dest.g() * dest.a() * (1. - source.a()))) / a;
|
||||
let b = ((source.b() * source.a()) + (dest.b() * dest.a() * (1. - source.a()))) / a;
|
||||
|
||||
Self(ColorF::new(r, g, b, a).to_u8())
|
||||
}
|
||||
|
||||
pub fn fade_out(&mut self, fade: f32) {
|
||||
let fade = fade.clamp(0., 1.);
|
||||
self.0.a = (self.0.a as f32 * (1. - fade)) as u8;
|
||||
pub fn transparent_black() -> Self {
|
||||
transparent_black()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Color {
|
||||
impl Eq for Hsla {}
|
||||
|
||||
pub fn hsla(h: f32, s: f32, l: f32, a: f32) -> Hsla {
|
||||
Hsla {
|
||||
h: h.clamp(0., 1.),
|
||||
s: s.clamp(0., 1.),
|
||||
l: l.clamp(0., 1.),
|
||||
a: a.clamp(0., 1.),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn black() -> Hsla {
|
||||
Hsla {
|
||||
h: 0.,
|
||||
s: 0.,
|
||||
l: 0.,
|
||||
a: 1.,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transparent_black() -> Hsla {
|
||||
Hsla {
|
||||
h: 0.,
|
||||
s: 0.,
|
||||
l: 0.,
|
||||
a: 0.,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn white() -> Hsla {
|
||||
Hsla {
|
||||
h: 0.,
|
||||
s: 0.,
|
||||
l: 1.,
|
||||
a: 1.,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn red() -> Hsla {
|
||||
Hsla {
|
||||
h: 0.,
|
||||
s: 1.,
|
||||
l: 0.5,
|
||||
a: 1.,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blue() -> Hsla {
|
||||
Hsla {
|
||||
h: 0.6,
|
||||
s: 1.,
|
||||
l: 0.5,
|
||||
a: 1.,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn green() -> Hsla {
|
||||
Hsla {
|
||||
h: 0.33,
|
||||
s: 1.,
|
||||
l: 0.5,
|
||||
a: 1.,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn yellow() -> Hsla {
|
||||
Hsla {
|
||||
h: 0.16,
|
||||
s: 1.,
|
||||
l: 0.5,
|
||||
a: 1.,
|
||||
}
|
||||
}
|
||||
|
||||
impl Hsla {
|
||||
/// Returns true if the HSLA color is fully transparent, false otherwise.
|
||||
pub fn is_transparent(&self) -> bool {
|
||||
self.a == 0.0
|
||||
}
|
||||
|
||||
/// Blends `other` on top of `self` based on `other`'s alpha value. The resulting color is a combination of `self`'s and `other`'s colors.
|
||||
///
|
||||
/// If `other`'s alpha value is 1.0 or greater, `other` color is fully opaque, thus `other` is returned as the output color.
|
||||
/// If `other`'s alpha value is 0.0 or less, `other` color is fully transparent, thus `self` is returned as the output color.
|
||||
/// Else, the output color is calculated as a blend of `self` and `other` based on their weighted alpha values.
|
||||
///
|
||||
/// Assumptions:
|
||||
/// - Alpha values are contained in the range [0, 1], with 1 as fully opaque and 0 as fully transparent.
|
||||
/// - The relative contributions of `self` and `other` is based on `self`'s alpha value (`self.a`) and `other`'s alpha value (`other.a`), `self` contributing `self.a * (1.0 - other.a)` and `other` contributing it's own alpha value.
|
||||
/// - RGB color components are contained in the range [0, 1].
|
||||
/// - If `self` and `other` colors are out of the valid range, the blend operation's output and behavior is undefined.
|
||||
pub fn blend(self, other: Hsla) -> Hsla {
|
||||
let alpha = other.a;
|
||||
|
||||
if alpha >= 1.0 {
|
||||
other
|
||||
} else if alpha <= 0.0 {
|
||||
return self;
|
||||
} else {
|
||||
let converted_self = Rgba::from(self);
|
||||
let converted_other = Rgba::from(other);
|
||||
let blended_rgb = converted_self.blend(converted_other);
|
||||
return Hsla::from(blended_rgb);
|
||||
}
|
||||
}
|
||||
|
||||
/// Fade out the color by a given factor. This factor should be between 0.0 and 1.0.
|
||||
/// Where 0.0 will leave the color unchanged, and 1.0 will completely fade out the color.
|
||||
pub fn fade_out(&mut self, factor: f32) {
|
||||
self.a *= 1.0 - factor.clamp(0., 1.);
|
||||
}
|
||||
}
|
||||
|
||||
// impl From<Hsla> for Rgba {
|
||||
// fn from(value: Hsla) -> Self {
|
||||
// let h = value.h;
|
||||
// let s = value.s;
|
||||
// let l = value.l;
|
||||
|
||||
// let c = (1 - |2L - 1|) X s
|
||||
// }
|
||||
// }
|
||||
|
||||
impl From<Rgba> for Hsla {
|
||||
fn from(color: Rgba) -> Self {
|
||||
let r = color.r;
|
||||
let g = color.g;
|
||||
let b = color.b;
|
||||
|
||||
let max = r.max(g.max(b));
|
||||
let min = r.min(g.min(b));
|
||||
let delta = max - min;
|
||||
|
||||
let l = (max + min) / 2.0;
|
||||
let s = if l == 0.0 || l == 1.0 {
|
||||
0.0
|
||||
} else if l < 0.5 {
|
||||
delta / (2.0 * l)
|
||||
} else {
|
||||
delta / (2.0 - 2.0 * l)
|
||||
};
|
||||
|
||||
let h = if delta == 0.0 {
|
||||
0.0
|
||||
} else if max == r {
|
||||
((g - b) / delta).rem_euclid(6.0) / 6.0
|
||||
} else if max == g {
|
||||
((b - r) / delta + 2.0) / 6.0
|
||||
} else {
|
||||
((r - g) / delta + 4.0) / 6.0
|
||||
};
|
||||
|
||||
Hsla {
|
||||
h,
|
||||
s,
|
||||
l,
|
||||
a: color.a,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Hsla {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let literal: Cow<str> = Deserialize::deserialize(deserializer)?;
|
||||
if let Some(digits) = literal.strip_prefix('#') {
|
||||
if let Ok(value) = u32::from_str_radix(digits, 16) {
|
||||
if digits.len() == 6 {
|
||||
return Ok(Color::from_u32((value << 8) | 0xFF));
|
||||
} else if digits.len() == 8 {
|
||||
return Ok(Color::from_u32(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(de::Error::invalid_value(
|
||||
Unexpected::Str(literal.as_ref()),
|
||||
&"#RRGGBB[AA]",
|
||||
))
|
||||
// First, deserialize it into Rgba
|
||||
let rgba = Rgba::deserialize(deserializer)?;
|
||||
|
||||
// Then, use the From<Rgba> for Hsla implementation to convert it
|
||||
Ok(Hsla::from(rgba))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u32> for Color {
|
||||
fn from(value: u32) -> Self {
|
||||
Self(ColorU::from_u32(value))
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
impl ToJson for Color {
|
||||
fn to_json(&self) -> serde_json::Value {
|
||||
json!(format!(
|
||||
"0x{:x}{:x}{:x}{:x}",
|
||||
self.0.r, self.0.g, self.0.b, self.0.a
|
||||
))
|
||||
}
|
||||
}
|
||||
use super::*;
|
||||
|
||||
impl Deref for Color {
|
||||
type Target = ColorU;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn test_deserialize_three_value_hex_to_rgba() {
|
||||
let actual: Rgba = serde_json::from_value(json!("#f09")).unwrap();
|
||||
|
||||
impl DerefMut for Color {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
assert_eq!(actual, rgba(0xff0099ff))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Color {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
#[test]
|
||||
fn test_deserialize_four_value_hex_to_rgba() {
|
||||
let actual: Rgba = serde_json::from_value(json!("#f09f")).unwrap();
|
||||
|
||||
assert_eq!(actual, rgba(0xff0099ff))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_six_value_hex_to_rgba() {
|
||||
let actual: Rgba = serde_json::from_value(json!("#ff0099")).unwrap();
|
||||
|
||||
assert_eq!(actual, rgba(0xff0099ff))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_eight_value_hex_to_rgba() {
|
||||
let actual: Rgba = serde_json::from_value(json!("#ff0099ff")).unwrap();
|
||||
|
||||
assert_eq!(actual, rgba(0xff0099ff))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_eight_value_hex_with_padding_to_rgba() {
|
||||
let actual: Rgba = serde_json::from_value(json!(" #f5f5f5ff ")).unwrap();
|
||||
|
||||
assert_eq!(actual, rgba(0xf5f5f5ff))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_eight_value_hex_with_mixed_case_to_rgba() {
|
||||
let actual: Rgba = serde_json::from_value(json!("#DeAdbEeF")).unwrap();
|
||||
|
||||
assert_eq!(actual, rgba(0xdeadbeef))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue