Rename Srgba to Color32

This commit is contained in:
Emil Ernerfeldt 2021-01-02 17:02:18 +01:00
parent 4fc12bf324
commit 73f3d8cf46
31 changed files with 198 additions and 193 deletions

View file

@ -15,8 +15,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed 🔧 ### Changed 🔧
* Renamed `Srgba` to `Color32`.
* Renamed `FontFamily::VariableWidth` to `FontFamily::Proportional`. * Renamed `FontFamily::VariableWidth` to `FontFamily::Proportional`.
* Remove `pixels_per_point` from `FontDefinitions`. * Removed `pixels_per_point` from `FontDefinitions`.
### Fixed 🐛 ### Fixed 🐛

View file

@ -9,7 +9,7 @@ pub struct Frame {
pub margin: Vec2, pub margin: Vec2,
pub corner_radius: f32, pub corner_radius: f32,
pub shadow: Shadow, pub shadow: Shadow,
pub fill: Srgba, pub fill: Color32,
pub stroke: Stroke, pub stroke: Stroke,
} }
@ -73,7 +73,7 @@ impl Frame {
Self { Self {
margin: Vec2::new(10.0, 10.0), margin: Vec2::new(10.0, 10.0),
corner_radius: 5.0, corner_radius: 5.0,
fill: Srgba::black_alpha(250), fill: Color32::black_alpha(250),
stroke: style.visuals.widgets.noninteractive.bg_stroke, stroke: style.visuals.widgets.noninteractive.bg_stroke,
..Default::default() ..Default::default()
} }
@ -81,7 +81,7 @@ impl Frame {
} }
impl Frame { impl Frame {
pub fn fill(mut self, fill: Srgba) -> Self { pub fn fill(mut self, fill: Color32) -> Self {
self.fill = fill; self.fill = fill;
self self
} }

View file

@ -109,7 +109,7 @@ pub use {
math::*, math::*,
memory::Memory, memory::Memory,
paint::{ paint::{
color, FontDefinitions, FontFamily, PaintCmd, PaintJobs, Rgba, Srgba, Stroke, TextStyle, color, Color32, FontDefinitions, FontFamily, PaintCmd, PaintJobs, Rgba, Stroke, TextStyle,
Texture, TextureId, Texture, TextureId,
}, },
painter::Painter, painter::Painter,

View file

@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};
use crate::{ use crate::{
area, collapsing_header, menu, area, collapsing_header, menu,
paint::color::{Hsva, Srgba}, paint::color::{Color32, Hsva},
resize, scroll_area, resize, scroll_area,
util::Cache, util::Cache,
widgets::text_edit, widgets::text_edit,
@ -47,7 +47,7 @@ pub struct Memory {
/// Used by color picker /// Used by color picker
#[cfg_attr(feature = "serde", serde(skip))] #[cfg_attr(feature = "serde", serde(skip))]
pub(crate) color_cache: Cache<Srgba, Hsva>, pub(crate) color_cache: Cache<Color32, Hsva>,
/// Which popup-window is open (if any)? /// Which popup-window is open (if any)?
/// Could be a combo box, color picker, menu etc. /// Could be a combo box, color picker, menu etc.

View file

@ -1,35 +1,35 @@
use crate::math::clamp; use crate::math::clamp;
/// This format is used for space-efficient color representation. /// This format is used for space-efficient color representation (32 bits).
/// ///
/// Instead of manipulating this directly it is often better /// Instead of manipulating this directly it is often better
/// to first convert it to either `Rgba` or `Hsva`. /// to first convert it to either [`Rgba`] or [`Hsva`].
/// ///
/// 0-255 gamma space `sRGBA` color with premultiplied alpha. /// Internally this uses 0-255 gamma space `sRGBA` color with premultiplied alpha.
/// Alpha channel is in linear space. /// Alpha channel is in linear space.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Srgba(pub(crate) [u8; 4]); pub struct Color32(pub(crate) [u8; 4]);
impl std::ops::Index<usize> for Srgba { impl std::ops::Index<usize> for Color32 {
type Output = u8; type Output = u8;
fn index(&self, index: usize) -> &u8 { fn index(&self, index: usize) -> &u8 {
&self.0[index] &self.0[index]
} }
} }
impl std::ops::IndexMut<usize> for Srgba { impl std::ops::IndexMut<usize> for Color32 {
fn index_mut(&mut self, index: usize) -> &mut u8 { fn index_mut(&mut self, index: usize) -> &mut u8 {
&mut self.0[index] &mut self.0[index]
} }
} }
// TODO: remove ? // TODO: remove ?
pub const fn srgba(r: u8, g: u8, b: u8, a: u8) -> Srgba { pub const fn srgba(r: u8, g: u8, b: u8, a: u8) -> Color32 {
Srgba::from_rgba_premultiplied(r, g, b, a) Color32::from_rgba_premultiplied(r, g, b, a)
} }
impl Srgba { impl Color32 {
#[deprecated = "Use from_rgb(..), from_rgba_premultiplied(..) or from_srgba_unmultiplied(..)"] #[deprecated = "Use from_rgb(..), from_rgba_premultiplied(..) or from_srgba_unmultiplied(..)"]
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self { pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self([r, g, b, a]) Self([r, g, b, a])
@ -108,16 +108,16 @@ impl Srgba {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
pub const TRANSPARENT: Srgba = srgba(0, 0, 0, 0); pub const TRANSPARENT: Color32 = srgba(0, 0, 0, 0);
pub const BLACK: Srgba = srgba(0, 0, 0, 255); pub const BLACK: Color32 = srgba(0, 0, 0, 255);
pub const LIGHT_GRAY: Srgba = srgba(220, 220, 220, 255); pub const LIGHT_GRAY: Color32 = srgba(220, 220, 220, 255);
pub const GRAY: Srgba = srgba(160, 160, 160, 255); pub const GRAY: Color32 = srgba(160, 160, 160, 255);
pub const WHITE: Srgba = srgba(255, 255, 255, 255); pub const WHITE: Color32 = srgba(255, 255, 255, 255);
pub const RED: Srgba = srgba(255, 0, 0, 255); pub const RED: Color32 = srgba(255, 0, 0, 255);
pub const GREEN: Srgba = srgba(0, 255, 0, 255); pub const GREEN: Color32 = srgba(0, 255, 0, 255);
pub const BLUE: Srgba = srgba(0, 0, 255, 255); pub const BLUE: Color32 = srgba(0, 0, 255, 255);
pub const YELLOW: Srgba = srgba(255, 255, 0, 255); pub const YELLOW: Color32 = srgba(255, 255, 0, 255);
pub const LIGHT_BLUE: Srgba = srgba(140, 160, 255, 255); pub const LIGHT_BLUE: Color32 = srgba(140, 160, 255, 255);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -273,30 +273,30 @@ impl std::ops::Mul<Rgba> for f32 {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Color conversion: // Color conversion:
impl From<Srgba> for Rgba { impl From<Color32> for Rgba {
fn from(srgba: Srgba) -> Rgba { fn from(srgba: Color32) -> Rgba {
Rgba([ Rgba([
linear_from_srgb_byte(srgba[0]), linear_from_gamma_byte(srgba[0]),
linear_from_srgb_byte(srgba[1]), linear_from_gamma_byte(srgba[1]),
linear_from_srgb_byte(srgba[2]), linear_from_gamma_byte(srgba[2]),
linear_from_alpha_byte(srgba[3]), linear_from_alpha_byte(srgba[3]),
]) ])
} }
} }
impl From<Rgba> for Srgba { impl From<Rgba> for Color32 {
fn from(rgba: Rgba) -> Srgba { fn from(rgba: Rgba) -> Color32 {
Srgba([ Color32([
srgb_byte_from_linear(rgba[0]), gamma_byte_from_linear(rgba[0]),
srgb_byte_from_linear(rgba[1]), gamma_byte_from_linear(rgba[1]),
srgb_byte_from_linear(rgba[2]), gamma_byte_from_linear(rgba[2]),
alpha_byte_from_linear(rgba[3]), alpha_byte_from_linear(rgba[3]),
]) ])
} }
} }
/// [0, 255] -> [0, 1] /// [0, 255] -> [0, 1]
fn linear_from_srgb_byte(s: u8) -> f32 { fn linear_from_gamma_byte(s: u8) -> f32 {
if s <= 10 { if s <= 10 {
s as f32 / 3294.6 s as f32 / 3294.6
} else { } else {
@ -309,7 +309,7 @@ fn linear_from_alpha_byte(a: u8) -> f32 {
} }
/// [0, 1] -> [0, 255] /// [0, 1] -> [0, 255]
fn srgb_byte_from_linear(l: f32) -> u8 { fn gamma_byte_from_linear(l: f32) -> u8 {
if l <= 0.0 { if l <= 0.0 {
0 0
} else if l <= 0.0031308 { } else if l <= 0.0031308 {
@ -329,9 +329,9 @@ fn alpha_byte_from_linear(a: f32) -> u8 {
fn test_srgba_conversion() { fn test_srgba_conversion() {
#![allow(clippy::float_cmp)] #![allow(clippy::float_cmp)]
for b in 0..=255 { for b in 0..=255 {
let l = linear_from_srgb_byte(b); let l = linear_from_gamma_byte(b);
assert!(0.0 <= l && l <= 1.0); assert!(0.0 <= l && l <= 1.0);
assert_eq!(srgb_byte_from_linear(l), b); assert_eq!(gamma_byte_from_linear(l), b);
} }
} }
@ -359,9 +359,9 @@ impl Hsva {
/// From `sRGBA` with premultiplied alpha /// From `sRGBA` with premultiplied alpha
pub fn from_srgba_premultiplied(srgba: [u8; 4]) -> Self { pub fn from_srgba_premultiplied(srgba: [u8; 4]) -> Self {
Self::from_rgba_premultiplied([ Self::from_rgba_premultiplied([
linear_from_srgb_byte(srgba[0]), linear_from_gamma_byte(srgba[0]),
linear_from_srgb_byte(srgba[1]), linear_from_gamma_byte(srgba[1]),
linear_from_srgb_byte(srgba[2]), linear_from_gamma_byte(srgba[2]),
linear_from_alpha_byte(srgba[3]), linear_from_alpha_byte(srgba[3]),
]) ])
} }
@ -369,9 +369,9 @@ impl Hsva {
/// From `sRGBA` without premultiplied alpha /// From `sRGBA` without premultiplied alpha
pub fn from_srgba_unmultiplied(srgba: [u8; 4]) -> Self { pub fn from_srgba_unmultiplied(srgba: [u8; 4]) -> Self {
Self::from_rgba_unmultiplied([ Self::from_rgba_unmultiplied([
linear_from_srgb_byte(srgba[0]), linear_from_gamma_byte(srgba[0]),
linear_from_srgb_byte(srgba[1]), linear_from_gamma_byte(srgba[1]),
linear_from_srgb_byte(srgba[2]), linear_from_gamma_byte(srgba[2]),
linear_from_alpha_byte(srgba[3]), linear_from_alpha_byte(srgba[3]),
]) ])
} }
@ -410,9 +410,9 @@ impl Hsva {
pub fn to_srgba_premultiplied(&self) -> [u8; 4] { pub fn to_srgba_premultiplied(&self) -> [u8; 4] {
let [r, g, b, a] = self.to_rgba_premultiplied(); let [r, g, b, a] = self.to_rgba_premultiplied();
[ [
srgb_byte_from_linear(r), gamma_byte_from_linear(r),
srgb_byte_from_linear(g), gamma_byte_from_linear(g),
srgb_byte_from_linear(b), gamma_byte_from_linear(b),
alpha_byte_from_linear(a), alpha_byte_from_linear(a),
] ]
} }
@ -420,9 +420,9 @@ impl Hsva {
pub fn to_srgba_unmultiplied(&self) -> [u8; 4] { pub fn to_srgba_unmultiplied(&self) -> [u8; 4] {
let [r, g, b, a] = self.to_rgba_unmultiplied(); let [r, g, b, a] = self.to_rgba_unmultiplied();
[ [
srgb_byte_from_linear(r), gamma_byte_from_linear(r),
srgb_byte_from_linear(g), gamma_byte_from_linear(g),
srgb_byte_from_linear(b), gamma_byte_from_linear(b),
alpha_byte_from_linear(a), alpha_byte_from_linear(a),
] ]
} }
@ -439,13 +439,13 @@ impl From<Rgba> for Hsva {
} }
} }
impl From<Hsva> for Srgba { impl From<Hsva> for Color32 {
fn from(hsva: Hsva) -> Srgba { fn from(hsva: Hsva) -> Color32 {
Srgba::from(Rgba::from(hsva)) Color32::from(Rgba::from(hsva))
} }
} }
impl From<Srgba> for Hsva { impl From<Color32> for Hsva {
fn from(srgba: Srgba) -> Hsva { fn from(srgba: Color32) -> Hsva {
Hsva::from(Rgba::from(srgba)) Hsva::from(Rgba::from(srgba))
} }
} }
@ -502,9 +502,9 @@ fn test_hsv_roundtrip() {
for r in 0..=255 { for r in 0..=255 {
for g in 0..=255 { for g in 0..=255 {
for b in 0..=255 { for b in 0..=255 {
let srgba = Srgba::from_rgb(r, g, b); let srgba = Color32::from_rgb(r, g, b);
let hsva = Hsva::from(srgba); let hsva = Hsva::from(srgba);
assert_eq!(srgba, Srgba::from(hsva)); assert_eq!(srgba, Color32::from(hsva));
} }
} }
} }

View file

@ -1,5 +1,5 @@
use { use {
super::{fonts::TextStyle, Fonts, Galley, Srgba, Triangles}, super::{fonts::TextStyle, Color32, Fonts, Galley, Triangles},
crate::{ crate::{
align::{anchor_rect, Align}, align::{anchor_rect, Align},
math::{Pos2, Rect}, math::{Pos2, Rect},
@ -19,7 +19,7 @@ pub enum PaintCmd {
Circle { Circle {
center: Pos2, center: Pos2,
radius: f32, radius: f32,
fill: Srgba, fill: Color32,
stroke: Stroke, stroke: Stroke,
}, },
LineSegment { LineSegment {
@ -31,14 +31,14 @@ pub enum PaintCmd {
/// If true, connect the first and last of the points together. /// If true, connect the first and last of the points together.
/// This is required if `fill != TRANSPARENT`. /// This is required if `fill != TRANSPARENT`.
closed: bool, closed: bool,
fill: Srgba, fill: Color32,
stroke: Stroke, stroke: Stroke,
}, },
Rect { Rect {
rect: Rect, rect: Rect,
/// How rounded the corners are. Use `0.0` for no rounding. /// How rounded the corners are. Use `0.0` for no rounding.
corner_radius: f32, corner_radius: f32,
fill: Srgba, fill: Color32,
stroke: Stroke, stroke: Stroke,
}, },
Text { Text {
@ -47,7 +47,7 @@ pub enum PaintCmd {
/// The layed out text /// The layed out text
galley: Galley, galley: Galley,
text_style: TextStyle, // TODO: Font? text_style: TextStyle, // TODO: Font?
color: Srgba, color: Color32,
}, },
Triangles(Triangles), Triangles(Triangles),
} }
@ -79,7 +79,7 @@ impl PaintCmd {
} }
} }
pub fn polygon(points: Vec<Pos2>, fill: impl Into<Srgba>, stroke: impl Into<Stroke>) -> Self { pub fn polygon(points: Vec<Pos2>, fill: impl Into<Color32>, stroke: impl Into<Stroke>) -> Self {
Self::Path { Self::Path {
points, points,
closed: true, closed: true,
@ -88,7 +88,7 @@ impl PaintCmd {
} }
} }
pub fn circle_filled(center: Pos2, radius: f32, fill_color: impl Into<Srgba>) -> Self { pub fn circle_filled(center: Pos2, radius: f32, fill_color: impl Into<Color32>) -> Self {
Self::Circle { Self::Circle {
center, center,
radius, radius,
@ -106,7 +106,7 @@ impl PaintCmd {
} }
} }
pub fn rect_filled(rect: Rect, corner_radius: f32, fill_color: impl Into<Srgba>) -> Self { pub fn rect_filled(rect: Rect, corner_radius: f32, fill_color: impl Into<Color32>) -> Self {
Self::Rect { Self::Rect {
rect, rect,
corner_radius, corner_radius,
@ -130,7 +130,7 @@ impl PaintCmd {
anchor: (Align, Align), anchor: (Align, Align),
text: impl Into<String>, text: impl Into<String>,
text_style: TextStyle, text_style: TextStyle,
color: Srgba, color: Color32,
) -> Self { ) -> Self {
let font = &fonts[text_style]; let font = &fonts[text_style];
let galley = font.layout_multiline(text.into(), f32::INFINITY); let galley = font.layout_multiline(text.into(), f32::INFINITY);
@ -199,7 +199,7 @@ impl PaintCmd {
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Stroke { pub struct Stroke {
pub width: f32, pub width: f32,
pub color: Srgba, pub color: Color32,
} }
impl Stroke { impl Stroke {
@ -207,7 +207,7 @@ impl Stroke {
Self::new(0.0, crate::color::TRANSPARENT) Self::new(0.0, crate::color::TRANSPARENT)
} }
pub fn new(width: impl Into<f32>, color: impl Into<Srgba>) -> Self { pub fn new(width: impl Into<f32>, color: impl Into<Color32>) -> Self {
Self { Self {
width: width.into(), width: width.into(),
color: color.into(), color: color.into(),
@ -217,7 +217,7 @@ impl Stroke {
impl<Color> From<(f32, Color)> for Stroke impl<Color> From<(f32, Color)> for Stroke
where where
Color: Into<Srgba>, Color: Into<Color32>,
{ {
fn from((width, color): (f32, Color)) -> Stroke { fn from((width, color): (f32, Color)) -> Stroke {
Stroke::new(width, color) Stroke::new(width, color)

View file

@ -11,7 +11,7 @@ pub mod tessellator;
mod texture_atlas; mod texture_atlas;
pub use { pub use {
color::{Rgba, Srgba}, color::{Color32, Rgba},
command::{PaintCmd, Stroke}, command::{PaintCmd, Stroke},
fonts::{FontDefinitions, FontFamily, Fonts, TextStyle}, fonts::{FontDefinitions, FontFamily, Fonts, TextStyle},
galley::*, galley::*,
@ -27,6 +27,6 @@ pub(crate) struct PaintRect {
pub rect: crate::Rect, pub rect: crate::Rect,
/// How rounded the corners are. Use `0.0` for no rounding. /// How rounded the corners are. Use `0.0` for no rounding.
pub corner_radius: f32, pub corner_radius: f32,
pub fill: Srgba, pub fill: Color32,
pub stroke: Stroke, pub stroke: Stroke,
} }

View file

@ -5,7 +5,7 @@ use super::*;
pub struct Shadow { pub struct Shadow {
// The shadow extends this much outside the rect. // The shadow extends this much outside the rect.
pub extrusion: f32, pub extrusion: f32,
pub color: Srgba, pub color: Color32,
} }
impl Shadow { impl Shadow {
@ -13,7 +13,7 @@ impl Shadow {
pub fn small() -> Self { pub fn small() -> Self {
Self { Self {
extrusion: 8.0, extrusion: 8.0,
color: Srgba::black_alpha(64), color: Color32::black_alpha(64),
} }
} }
@ -21,7 +21,7 @@ impl Shadow {
pub fn big() -> Self { pub fn big() -> Self {
Self { Self {
extrusion: 32.0, extrusion: 32.0,
color: Srgba::black_alpha(96), color: Color32::black_alpha(96),
} }
} }

View file

@ -7,7 +7,7 @@
use { use {
super::{ super::{
color::{self, srgba, Rgba, Srgba, TRANSPARENT}, color::{self, srgba, Color32, Rgba, TRANSPARENT},
*, *,
}, },
crate::math::*, crate::math::*,
@ -54,7 +54,7 @@ pub struct Vertex {
pub uv: Pos2, // 64 bit pub uv: Pos2, // 64 bit
/// sRGBA with premultiplied alpha /// sRGBA with premultiplied alpha
pub color: Srgba, // 32 bit pub color: Color32, // 32 bit
} }
/// Textured triangles. /// Textured triangles.
@ -123,7 +123,7 @@ impl Triangles {
} }
} }
pub fn colored_vertex(&mut self, pos: Pos2, color: Srgba) { pub fn colored_vertex(&mut self, pos: Pos2, color: Color32) {
debug_assert!(self.texture_id == TextureId::Egui); debug_assert!(self.texture_id == TextureId::Egui);
self.vertices.push(Vertex { self.vertices.push(Vertex {
pos, pos,
@ -152,7 +152,7 @@ impl Triangles {
} }
/// Rectangle with a texture and color. /// Rectangle with a texture and color.
pub fn add_rect_with_uv(&mut self, pos: Rect, uv: Rect, color: Srgba) { pub fn add_rect_with_uv(&mut self, pos: Rect, uv: Rect, color: Color32) {
let idx = self.vertices.len() as u32; let idx = self.vertices.len() as u32;
self.add_triangle(idx + 0, idx + 1, idx + 2); self.add_triangle(idx + 0, idx + 1, idx + 2);
self.add_triangle(idx + 2, idx + 1, idx + 3); self.add_triangle(idx + 2, idx + 1, idx + 3);
@ -184,7 +184,7 @@ impl Triangles {
} }
/// Uniformly colored rectangle. /// Uniformly colored rectangle.
pub fn add_colored_rect(&mut self, rect: Rect, color: Srgba) { pub fn add_colored_rect(&mut self, rect: Rect, color: Color32) {
debug_assert!(self.texture_id == TextureId::Egui); debug_assert!(self.texture_id == TextureId::Egui);
self.add_rect_with_uv(rect, [WHITE_UV, WHITE_UV].into(), color) self.add_rect_with_uv(rect, [WHITE_UV, WHITE_UV].into(), color)
} }
@ -475,7 +475,7 @@ impl Default for TessellationOptions {
/// Tessellate the given convex area into a polygon. /// Tessellate the given convex area into a polygon.
fn fill_closed_path( fn fill_closed_path(
path: &[PathPoint], path: &[PathPoint],
color: Srgba, color: Color32,
options: TessellationOptions, options: TessellationOptions,
out: &mut Triangles, out: &mut Triangles,
) { ) {
@ -656,7 +656,7 @@ fn stroke_path(
} }
} }
fn mul_color(color: Srgba, factor: f32) -> Srgba { fn mul_color(color: Color32, factor: f32) -> Color32 {
debug_assert!(0.0 <= factor && factor <= 1.0); debug_assert!(0.0 <= factor && factor <= 1.0);
// sRGBA correct fading requires conversion to linear space and back again because of premultiplied alpha // sRGBA correct fading requires conversion to linear space and back again because of premultiplied alpha
Rgba::from(color).multiply(factor).into() Rgba::from(color).multiply(factor).into()
@ -828,7 +828,7 @@ impl Tessellator {
pos: Pos2, pos: Pos2,
galley: &super::Galley, galley: &super::Galley,
text_style: super::TextStyle, text_style: super::TextStyle,
color: Srgba, color: Color32,
out: &mut Triangles, out: &mut Triangles,
) { ) {
if color == TRANSPARENT { if color == TRANSPARENT {

View file

@ -13,9 +13,9 @@ pub struct Texture {
impl Texture { impl Texture {
/// Returns the textures as `sRGBA` premultiplied pixels, row by row, top to bottom. /// Returns the textures as `sRGBA` premultiplied pixels, row by row, top to bottom.
pub fn srgba_pixels(&'_ self) -> impl Iterator<Item = super::Srgba> + '_ { pub fn srgba_pixels(&'_ self) -> impl Iterator<Item = super::Color32> + '_ {
use super::Srgba; use super::Color32;
let srgba_from_luminance_lut: Vec<Srgba> = (0..=255).map(Srgba::white_alpha).collect(); let srgba_from_luminance_lut: Vec<Color32> = (0..=255).map(Color32::white_alpha).collect();
self.pixels self.pixels
.iter() .iter()
.map(move |&l| srgba_from_luminance_lut[l as usize]) .map(move |&l| srgba_from_luminance_lut[l as usize])

View file

@ -4,7 +4,7 @@ use crate::{
layers::PaintCmdIdx, layers::PaintCmdIdx,
math::{Pos2, Rect, Vec2}, math::{Pos2, Rect, Vec2},
paint::{Fonts, Galley, PaintCmd, Stroke, TextStyle}, paint::{Fonts, Galley, PaintCmd, Stroke, TextStyle},
CtxRef, LayerId, Srgba, Color32, CtxRef, LayerId,
}; };
/// Helper to paint shapes and text to a specific region on a specific layer. /// Helper to paint shapes and text to a specific region on a specific layer.
@ -133,7 +133,7 @@ impl Painter {
/// ## Debug painting /// ## Debug painting
impl Painter { impl Painter {
pub fn debug_rect(&mut self, rect: Rect, color: Srgba, text: impl Into<String>) { pub fn debug_rect(&mut self, rect: Rect, color: Color32, text: impl Into<String>) {
self.rect_stroke(rect, 0.0, (1.0, color)); self.rect_stroke(rect, 0.0, (1.0, color));
let text_style = TextStyle::Monospace; let text_style = TextStyle::Monospace;
self.text(rect.min, LEFT_TOP, text.into(), text_style, color); self.text(rect.min, LEFT_TOP, text.into(), text_style, color);
@ -148,7 +148,7 @@ impl Painter {
self.add(PaintCmd::Rect { self.add(PaintCmd::Rect {
rect: frame_rect, rect: frame_rect,
corner_radius: 0.0, corner_radius: 0.0,
fill: Srgba::black_alpha(240), fill: Color32::black_alpha(240),
stroke: Stroke::new(1.0, color::RED), stroke: Stroke::new(1.0, color::RED),
}); });
self.galley(rect.min, galley, text_style, color::RED); self.galley(rect.min, galley, text_style, color::RED);
@ -169,7 +169,7 @@ impl Painter {
&self, &self,
center: Pos2, center: Pos2,
radius: f32, radius: f32,
fill_color: impl Into<Srgba>, fill_color: impl Into<Color32>,
stroke: impl Into<Stroke>, stroke: impl Into<Stroke>,
) { ) {
self.add(PaintCmd::Circle { self.add(PaintCmd::Circle {
@ -180,7 +180,7 @@ impl Painter {
}); });
} }
pub fn circle_filled(&self, center: Pos2, radius: f32, fill_color: impl Into<Srgba>) { pub fn circle_filled(&self, center: Pos2, radius: f32, fill_color: impl Into<Color32>) {
self.add(PaintCmd::Circle { self.add(PaintCmd::Circle {
center, center,
radius, radius,
@ -202,7 +202,7 @@ impl Painter {
&self, &self,
rect: Rect, rect: Rect,
corner_radius: f32, corner_radius: f32,
fill_color: impl Into<Srgba>, fill_color: impl Into<Color32>,
stroke: impl Into<Stroke>, stroke: impl Into<Stroke>,
) { ) {
self.add(PaintCmd::Rect { self.add(PaintCmd::Rect {
@ -213,7 +213,7 @@ impl Painter {
}); });
} }
pub fn rect_filled(&self, rect: Rect, corner_radius: f32, fill_color: impl Into<Srgba>) { pub fn rect_filled(&self, rect: Rect, corner_radius: f32, fill_color: impl Into<Color32>) {
self.add(PaintCmd::Rect { self.add(PaintCmd::Rect {
rect, rect,
corner_radius, corner_radius,
@ -257,7 +257,7 @@ impl Painter {
anchor: (Align, Align), anchor: (Align, Align),
text: impl Into<String>, text: impl Into<String>,
text_style: TextStyle, text_style: TextStyle,
text_color: Srgba, text_color: Color32,
) -> Rect { ) -> Rect {
let font = &self.fonts()[text_style]; let font = &self.fonts()[text_style];
let galley = font.layout_multiline(text.into(), f32::INFINITY); let galley = font.layout_multiline(text.into(), f32::INFINITY);
@ -267,7 +267,7 @@ impl Painter {
} }
/// Paint text that has already been layed out in a `Galley`. /// Paint text that has already been layed out in a `Galley`.
pub fn galley(&self, pos: Pos2, galley: Galley, text_style: TextStyle, color: Srgba) { pub fn galley(&self, pos: Pos2, galley: Galley, text_style: TextStyle, color: Color32) {
self.add(PaintCmd::Text { self.add(PaintCmd::Text {
pos, pos,
galley, galley,

View file

@ -118,7 +118,7 @@ pub struct Visuals {
/// so that `visuals.text_color` is always used, /// so that `visuals.text_color` is always used,
/// but its alpha may be different based on whether or not /// but its alpha may be different based on whether or not
/// it is disabled, non-interactive, hovered etc. /// it is disabled, non-interactive, hovered etc.
pub override_text_color: Option<Srgba>, pub override_text_color: Option<Color32>,
/// Visual styles of widgets /// Visual styles of widgets
pub widgets: Widgets, pub widgets: Widgets,
@ -127,10 +127,10 @@ pub struct Visuals {
/// e.g. the background of the slider or text edit, /// e.g. the background of the slider or text edit,
/// needs to look different from other interactive stuff. /// needs to look different from other interactive stuff.
pub dark_bg_color: Srgba, // TODO: remove, rename, or clarify what it is for pub dark_bg_color: Color32, // TODO: remove, rename, or clarify what it is for
/// The color used for `Hyperlink`, /// The color used for `Hyperlink`,
pub hyperlink_color: Srgba, pub hyperlink_color: Color32,
pub window_corner_radius: f32, pub window_corner_radius: f32,
pub window_shadow: Shadow, pub window_shadow: Shadow,
@ -156,7 +156,7 @@ impl Visuals {
&self.widgets.noninteractive &self.widgets.noninteractive
} }
pub fn text_color(&self) -> Srgba { pub fn text_color(&self) -> Color32 {
self.override_text_color self.override_text_color
.unwrap_or_else(|| self.widgets.noninteractive.text_color()) .unwrap_or_else(|| self.widgets.noninteractive.text_color())
} }
@ -166,7 +166,7 @@ impl Visuals {
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Selection { pub struct Selection {
pub bg_fill: Srgba, pub bg_fill: Color32,
pub stroke: Stroke, pub stroke: Stroke,
} }
@ -204,7 +204,7 @@ impl Widgets {
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct WidgetVisuals { pub struct WidgetVisuals {
/// Background color of widget /// Background color of widget
pub bg_fill: Srgba, pub bg_fill: Color32,
/// For surrounding rectangle of things that need it, /// For surrounding rectangle of things that need it,
/// like buttons, the box of the checkbox, etc. /// like buttons, the box of the checkbox, etc.
@ -215,14 +215,14 @@ pub struct WidgetVisuals {
/// Fill color of the interactive part of a component (slider grab, checkbox, ...) /// Fill color of the interactive part of a component (slider grab, checkbox, ...)
/// When you need a fill. /// When you need a fill.
pub fg_fill: Srgba, pub fg_fill: Color32,
/// Stroke and text color of the interactive part of a component (button text, slider grab, check-mark, ...) /// Stroke and text color of the interactive part of a component (button text, slider grab, check-mark, ...)
pub fg_stroke: Stroke, pub fg_stroke: Stroke,
} }
impl WidgetVisuals { impl WidgetVisuals {
pub fn text_color(&self) -> Srgba { pub fn text_color(&self) -> Color32 {
self.fg_stroke.color self.fg_stroke.color
} }
} }
@ -273,8 +273,8 @@ impl Default for Visuals {
override_text_color: None, override_text_color: None,
widgets: Default::default(), widgets: Default::default(),
selection: Default::default(), selection: Default::default(),
dark_bg_color: Srgba::black_alpha(140), dark_bg_color: Color32::black_alpha(140),
hyperlink_color: Srgba::from_rgb(90, 170, 255), hyperlink_color: Color32::from_rgb(90, 170, 255),
window_corner_radius: 10.0, window_corner_radius: 10.0,
window_shadow: Shadow::big(), window_shadow: Shadow::big(),
resize_corner_size: 12.0, resize_corner_size: 12.0,
@ -311,28 +311,28 @@ impl Default for Widgets {
bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.5)), bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.5)),
corner_radius: 4.0, corner_radius: 4.0,
fg_fill: srgba(100, 100, 150, 255), fg_fill: srgba(100, 100, 150, 255),
fg_stroke: Stroke::new(1.5, Srgba::gray(240)), fg_stroke: Stroke::new(1.5, Color32::gray(240)),
}, },
inactive: WidgetVisuals { inactive: WidgetVisuals {
bg_fill: Rgba::luminance_alpha(0.04, 0.5).into(), bg_fill: Rgba::luminance_alpha(0.04, 0.5).into(),
bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.06)), // default window outline. Should be pretty readable bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.06)), // default window outline. Should be pretty readable
corner_radius: 4.0, corner_radius: 4.0,
fg_fill: srgba(60, 60, 80, 255), fg_fill: srgba(60, 60, 80, 255),
fg_stroke: Stroke::new(1.0, Srgba::gray(200)), // Should NOT look grayed out! fg_stroke: Stroke::new(1.0, Color32::gray(200)), // Should NOT look grayed out!
}, },
disabled: WidgetVisuals { disabled: WidgetVisuals {
bg_fill: Rgba::luminance_alpha(0.02, 0.5).into(), bg_fill: Rgba::luminance_alpha(0.02, 0.5).into(),
bg_stroke: Stroke::new(0.5, Srgba::gray(70)), bg_stroke: Stroke::new(0.5, Color32::gray(70)),
corner_radius: 4.0, corner_radius: 4.0,
fg_fill: srgba(50, 50, 50, 255), fg_fill: srgba(50, 50, 50, 255),
fg_stroke: Stroke::new(1.0, Srgba::gray(140)), // Should look grayed out fg_stroke: Stroke::new(1.0, Color32::gray(140)), // Should look grayed out
}, },
noninteractive: WidgetVisuals { noninteractive: WidgetVisuals {
bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.06)), bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.06)),
bg_fill: Rgba::luminance_alpha(0.010, 0.975).into(), // window background bg_fill: Rgba::luminance_alpha(0.010, 0.975).into(), // window background
corner_radius: 4.0, corner_radius: 4.0,
fg_fill: Default::default(), fg_fill: Default::default(),
fg_stroke: Stroke::new(1.0, Srgba::gray(160)), // text color fg_stroke: Stroke::new(1.0, Color32::gray(160)), // text color
}, },
} }
} }
@ -577,7 +577,7 @@ fn ui_slider_vec2(
.1 .1
} }
fn ui_color(ui: &mut Ui, srgba: &mut Srgba, text: &str) { fn ui_color(ui: &mut Ui, srgba: &mut Color32, text: &str) {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.color_edit_button_srgba(srgba); ui.color_edit_button_srgba(srgba);
ui.label(text); ui.label(text);

View file

@ -502,7 +502,7 @@ impl Ui {
if (debug_expand_width && too_wide) || (debug_expand_height && too_high) { if (debug_expand_width && too_wide) || (debug_expand_height && too_high) {
self.painter.rect_stroke(rect, 0.0, (1.0, LIGHT_BLUE)); self.painter.rect_stroke(rect, 0.0, (1.0, LIGHT_BLUE));
let color = color::Srgba::from_rgb(200, 0, 0); let color = color::Color32::from_rgb(200, 0, 0);
let width = 2.5; let width = 2.5;
let paint_line_seg = |a, b| self.painter().line_segment([a, b], (width, color)); let paint_line_seg = |a, b| self.painter().line_segment([a, b], (width, color));
@ -649,7 +649,11 @@ impl Ui {
} }
/// Shortcut for `add(Label::new(text).text_color(color))` /// Shortcut for `add(Label::new(text).text_color(color))`
pub fn colored_label(&mut self, color: impl Into<Srgba>, label: impl Into<Label>) -> Response { pub fn colored_label(
&mut self,
color: impl Into<Color32>,
label: impl Into<Label>,
) -> Response {
self.add(label.into().text_color(color)) self.add(label.into().text_color(color))
} }
@ -808,7 +812,7 @@ impl Ui {
impl Ui { impl Ui {
/// Shows a button with the given color. /// Shows a button with the given color.
/// If the user clicks the button, a full color picker is shown. /// If the user clicks the button, a full color picker is shown.
pub fn color_edit_button_srgba(&mut self, srgba: &mut Srgba) -> Response { pub fn color_edit_button_srgba(&mut self, srgba: &mut Color32) -> Response {
widgets::color_picker::color_edit_button_srgba(self, srgba) widgets::color_picker::color_edit_button_srgba(self, srgba)
} }
@ -822,7 +826,7 @@ impl Ui {
/// If the user clicks the button, a full color picker is shown. /// If the user clicks the button, a full color picker is shown.
/// The given color is in `sRGBA` space with premultiplied alpha /// The given color is in `sRGBA` space with premultiplied alpha
pub fn color_edit_button_srgba_premultiplied(&mut self, srgba: &mut [u8; 4]) -> Response { pub fn color_edit_button_srgba_premultiplied(&mut self, srgba: &mut [u8; 4]) -> Response {
let mut color = Srgba(*srgba); let mut color = Color32(*srgba);
let response = self.color_edit_button_srgba(&mut color); let response = self.color_edit_button_srgba(&mut color);
*srgba = color.0; *srgba = color.0;
response response

View file

@ -27,7 +27,7 @@ impl ImageButton {
} }
/// Multiply image color with this. Default is WHITE (no tint). /// Multiply image color with this. Default is WHITE (no tint).
pub fn tint(mut self, tint: impl Into<Srgba>) -> Self { pub fn tint(mut self, tint: impl Into<Color32>) -> Self {
self.image = self.image.tint(tint); self.image = self.image.tint(tint);
self self
} }

View file

@ -5,7 +5,7 @@ use crate::{
*, *,
}; };
fn contrast_color(color: impl Into<Rgba>) -> Srgba { fn contrast_color(color: impl Into<Rgba>) -> Color32 {
if color.into().intensity() < 0.5 { if color.into().intensity() < 0.5 {
color::WHITE color::WHITE
} else { } else {
@ -19,8 +19,8 @@ fn contrast_color(color: impl Into<Rgba>) -> Srgba {
const N: u32 = 6 * 3; const N: u32 = 6 * 3;
fn background_checkers(painter: &Painter, rect: Rect) { fn background_checkers(painter: &Painter, rect: Rect) {
let mut top_color = Srgba::gray(128); let mut top_color = Color32::gray(128);
let mut bottom_color = Srgba::gray(32); let mut bottom_color = Color32::gray(32);
let checker_size = Vec2::splat(rect.height() / 2.0); let checker_size = Vec2::splat(rect.height() / 2.0);
let n = (rect.width() / checker_size.x).round() as u32; let n = (rect.width() / checker_size.x).round() as u32;
@ -40,11 +40,11 @@ fn background_checkers(painter: &Painter, rect: Rect) {
painter.add(PaintCmd::triangles(triangles)); painter.add(PaintCmd::triangles(triangles));
} }
pub fn show_color(ui: &mut Ui, color: impl Into<Srgba>, desired_size: Vec2) -> Response { pub fn show_color(ui: &mut Ui, color: impl Into<Color32>, desired_size: Vec2) -> Response {
show_srgba(ui, color.into(), desired_size) show_srgba(ui, color.into(), desired_size)
} }
fn show_srgba(ui: &mut Ui, srgba: Srgba, desired_size: Vec2) -> Response { fn show_srgba(ui: &mut Ui, srgba: Color32, desired_size: Vec2) -> Response {
let response = ui.allocate_response(desired_size, Sense::hover()); let response = ui.allocate_response(desired_size, Sense::hover());
background_checkers(ui.painter(), response.rect); background_checkers(ui.painter(), response.rect);
ui.painter().add(PaintCmd::Rect { ui.painter().add(PaintCmd::Rect {
@ -56,7 +56,7 @@ fn show_srgba(ui: &mut Ui, srgba: Srgba, desired_size: Vec2) -> Response {
response response
} }
fn color_button(ui: &mut Ui, color: Srgba) -> Response { fn color_button(ui: &mut Ui, color: Color32) -> Response {
let desired_size = ui.style().spacing.interact_size; let desired_size = ui.style().spacing.interact_size;
let response = ui.allocate_response(desired_size, Sense::click()); let response = ui.allocate_response(desired_size, Sense::click());
let visuals = ui.style().interact(&response); let visuals = ui.style().interact(&response);
@ -70,7 +70,7 @@ fn color_button(ui: &mut Ui, color: Srgba) -> Response {
response response
} }
fn color_slider_1d(ui: &mut Ui, value: &mut f32, color_at: impl Fn(f32) -> Srgba) -> Response { fn color_slider_1d(ui: &mut Ui, value: &mut f32, color_at: impl Fn(f32) -> Color32) -> Response {
#![allow(clippy::identity_op)] #![allow(clippy::identity_op)]
let desired_size = vec2( let desired_size = vec2(
@ -132,7 +132,7 @@ fn color_slider_2d(
ui: &mut Ui, ui: &mut Ui,
x_value: &mut f32, x_value: &mut f32,
y_value: &mut f32, y_value: &mut f32,
color_at: impl Fn(f32, f32) -> Srgba, color_at: impl Fn(f32, f32) -> Color32,
) -> Response { ) -> Response {
let desired_size = Vec2::splat(ui.style().spacing.slider_width); let desired_size = Vec2::splat(ui.style().spacing.slider_width);
let response = ui.allocate_response(desired_size, Sense::click_and_drag()); let response = ui.allocate_response(desired_size, Sense::click_and_drag());
@ -246,7 +246,7 @@ pub fn color_edit_button_hsva(ui: &mut Ui, hsva: &mut Hsva) -> Response {
/// Shows a button with the given color. /// Shows a button with the given color.
/// If the user clicks the button, a full color picker is shown. /// If the user clicks the button, a full color picker is shown.
pub fn color_edit_button_srgba(ui: &mut Ui, srgba: &mut Srgba) -> Response { pub fn color_edit_button_srgba(ui: &mut Ui, srgba: &mut Color32) -> Response {
// To ensure we keep hue slider when `srgba` is grey we store the // To ensure we keep hue slider when `srgba` is grey we store the
// full `Hsva` in a cache: // full `Hsva` in a cache:
@ -260,7 +260,7 @@ pub fn color_edit_button_srgba(ui: &mut Ui, srgba: &mut Srgba) -> Response {
let response = color_edit_button_hsva(ui, &mut hsva); let response = color_edit_button_hsva(ui, &mut hsva);
*srgba = Srgba::from(hsva); *srgba = Color32::from(hsva);
ui.ctx().memory().color_cache.set(*srgba, hsva); ui.ctx().memory().color_cache.set(*srgba, hsva);
@ -297,8 +297,8 @@ impl From<HsvaGamma> for Rgba {
} }
} }
impl From<HsvaGamma> for Srgba { impl From<HsvaGamma> for Color32 {
fn from(hsvag: HsvaGamma) -> Srgba { fn from(hsvag: HsvaGamma) -> Color32 {
Rgba::from(hsvag).into() Rgba::from(hsvag).into()
} }
} }

View file

@ -7,8 +7,8 @@ pub struct Image {
texture_id: TextureId, texture_id: TextureId,
uv: Rect, uv: Rect,
desired_size: Vec2, desired_size: Vec2,
bg_fill: Srgba, bg_fill: Color32,
tint: Srgba, tint: Color32,
} }
impl Image { impl Image {
@ -29,13 +29,13 @@ impl Image {
} }
/// A solid color to put behind the image. Useful for transparent images. /// A solid color to put behind the image. Useful for transparent images.
pub fn bg_fill(mut self, bg_fill: impl Into<Srgba>) -> Self { pub fn bg_fill(mut self, bg_fill: impl Into<Color32>) -> Self {
self.bg_fill = bg_fill.into(); self.bg_fill = bg_fill.into();
self self
} }
/// Multiply image color with this. Default is WHITE (no tint). /// Multiply image color with this. Default is WHITE (no tint).
pub fn tint(mut self, tint: impl Into<Srgba>) -> Self { pub fn tint(mut self, tint: impl Into<Color32>) -> Self {
self.tint = tint.into(); self.tint = tint.into();
self self
} }

View file

@ -37,7 +37,7 @@ pub struct Label {
pub(crate) text: String, pub(crate) text: String,
pub(crate) multiline: Option<bool>, pub(crate) multiline: Option<bool>,
pub(crate) text_style: Option<TextStyle>, pub(crate) text_style: Option<TextStyle>,
pub(crate) text_color: Option<Srgba>, pub(crate) text_color: Option<Color32>,
} }
impl Label { impl Label {
@ -83,7 +83,7 @@ impl Label {
self.text_style(TextStyle::Small) self.text_style(TextStyle::Small)
} }
pub fn text_color(mut self, text_color: impl Into<Srgba>) -> Self { pub fn text_color(mut self, text_color: impl Into<Color32>) -> Self {
self.text_color = Some(text_color.into()); self.text_color = Some(text_color.into());
self self
} }
@ -303,10 +303,10 @@ impl Widget for Hyperlink {
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"] #[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
pub struct Button { pub struct Button {
text: String, text: String,
text_color: Option<Srgba>, text_color: Option<Color32>,
text_style: TextStyle, text_style: TextStyle,
/// None means default for interact /// None means default for interact
fill: Option<Srgba>, fill: Option<Color32>,
sense: Sense, sense: Sense,
small: bool, small: bool,
frame: bool, frame: bool,
@ -325,12 +325,12 @@ impl Button {
} }
} }
pub fn text_color(mut self, text_color: Srgba) -> Self { pub fn text_color(mut self, text_color: Color32) -> Self {
self.text_color = Some(text_color); self.text_color = Some(text_color);
self self
} }
pub fn text_color_opt(mut self, text_color: Option<Srgba>) -> Self { pub fn text_color_opt(mut self, text_color: Option<Color32>) -> Self {
self.text_color = text_color; self.text_color = text_color;
self self
} }
@ -340,7 +340,7 @@ impl Button {
self self
} }
pub fn fill(mut self, fill: Option<Srgba>) -> Self { pub fn fill(mut self, fill: Option<Color32>) -> Self {
self.fill = fill; self.fill = fill;
self self
} }
@ -443,7 +443,7 @@ impl Widget for Button {
pub struct Checkbox<'a> { pub struct Checkbox<'a> {
checked: &'a mut bool, checked: &'a mut bool,
text: String, text: String,
text_color: Option<Srgba>, text_color: Option<Color32>,
} }
impl<'a> Checkbox<'a> { impl<'a> Checkbox<'a> {
@ -455,7 +455,7 @@ impl<'a> Checkbox<'a> {
} }
} }
pub fn text_color(mut self, text_color: Srgba) -> Self { pub fn text_color(mut self, text_color: Color32) -> Self {
self.text_color = Some(text_color); self.text_color = Some(text_color);
self self
} }
@ -538,7 +538,7 @@ impl<'a> Widget for Checkbox<'a> {
pub struct RadioButton { pub struct RadioButton {
checked: bool, checked: bool,
text: String, text: String,
text_color: Option<Srgba>, text_color: Option<Color32>,
} }
impl RadioButton { impl RadioButton {
@ -550,7 +550,7 @@ impl RadioButton {
} }
} }
pub fn text_color(mut self, text_color: Srgba) -> Self { pub fn text_color(mut self, text_color: Color32) -> Self {
self.text_color = Some(text_color); self.text_color = Some(text_color);
self self
} }

View file

@ -44,7 +44,7 @@ pub struct Slider<'a> {
smart_aim: bool, smart_aim: bool,
// TODO: label: Option<Label> // TODO: label: Option<Label>
text: Option<String>, text: Option<String>,
text_color: Option<Srgba>, text_color: Option<Color32>,
min_decimals: usize, min_decimals: usize,
max_decimals: Option<usize>, max_decimals: Option<usize>,
} }
@ -133,7 +133,7 @@ impl<'a> Slider<'a> {
self self
} }
pub fn text_color(mut self, text_color: Srgba) -> Self { pub fn text_color(mut self, text_color: Color32) -> Self {
self.text_color = Some(text_color); self.text_color = Some(text_color);
self self
} }

View file

@ -121,7 +121,7 @@ pub struct TextEdit<'t> {
id: Option<Id>, id: Option<Id>,
id_source: Option<Id>, id_source: Option<Id>,
text_style: Option<TextStyle>, text_style: Option<TextStyle>,
text_color: Option<Srgba>, text_color: Option<Color32>,
multiline: bool, multiline: bool,
enabled: bool, enabled: bool,
desired_width: Option<f32>, desired_width: Option<f32>,
@ -180,12 +180,12 @@ impl<'t> TextEdit<'t> {
self self
} }
pub fn text_color(mut self, text_color: Srgba) -> Self { pub fn text_color(mut self, text_color: Color32) -> Self {
self.text_color = Some(text_color); self.text_color = Some(text_color);
self self
} }
pub fn text_color_opt(mut self, text_color: Option<Srgba>) -> Self { pub fn text_color_opt(mut self, text_color: Option<Color32>) -> Self {
self.text_color = text_color; self.text_color = text_color;
self self
} }

View file

@ -62,7 +62,7 @@ impl ColorTest {
ui.label("Use a color picker to ensure this color is (255, 165, 0) / #ffa500"); ui.label("Use a color picker to ensure this color is (255, 165, 0) / #ffa500");
ui.wrap(|ui| { ui.wrap(|ui| {
ui.style_mut().spacing.item_spacing.y = 0.0; // No spacing between gradients ui.style_mut().spacing.item_spacing.y = 0.0; // No spacing between gradients
let g = Gradient::one_color(Srgba::from_rgb(255, 165, 0)); let g = Gradient::one_color(Color32::from_rgb(255, 165, 0));
self.vertex_gradient(ui, "orange rgb(255, 165, 0) - vertex", WHITE, &g); self.vertex_gradient(ui, "orange rgb(255, 165, 0) - vertex", WHITE, &g);
self.tex_gradient( self.tex_gradient(
ui, ui,
@ -91,13 +91,13 @@ impl ColorTest {
ui.label(" vertex color ="); ui.label(" vertex color =");
}); });
{ {
let g = Gradient::one_color(Srgba::from(tex_color * vertex_color)); let g = Gradient::one_color(Color32::from(tex_color * vertex_color));
self.vertex_gradient(ui, "Ground truth (vertices)", WHITE, &g); self.vertex_gradient(ui, "Ground truth (vertices)", WHITE, &g);
self.tex_gradient(ui, tex_allocator, "Ground truth (texture)", WHITE, &g); self.tex_gradient(ui, tex_allocator, "Ground truth (texture)", WHITE, &g);
} }
if let Some(tex_allocator) = &mut tex_allocator { if let Some(tex_allocator) = &mut tex_allocator {
ui.horizontal(|ui| { ui.horizontal(|ui| {
let g = Gradient::one_color(Srgba::from(tex_color)); let g = Gradient::one_color(Color32::from(tex_color));
let tex = self.tex_mngr.get(*tex_allocator, &g); let tex = self.tex_mngr.get(*tex_allocator, &g);
let texel_offset = 0.5 / (g.0.len() as f32); let texel_offset = 0.5 / (g.0.len() as f32);
let uv = let uv =
@ -146,7 +146,7 @@ impl ColorTest {
ui, ui,
tex_allocator, tex_allocator,
RED, RED,
(TRANSPARENT, Srgba::from_rgba_premultiplied(0, 0, 255, 0)), (TRANSPARENT, Color32::from_rgba_premultiplied(0, 0, 255, 0)),
); );
ui.separator(); ui.separator();
@ -156,8 +156,8 @@ impl ColorTest {
&mut self, &mut self,
ui: &mut Ui, ui: &mut Ui,
tex_allocator: &mut Option<&mut dyn epi::TextureAllocator>, tex_allocator: &mut Option<&mut dyn epi::TextureAllocator>,
bg_fill: Srgba, bg_fill: Color32,
(left, right): (Srgba, Srgba), (left, right): (Color32, Color32),
) { ) {
let is_opaque = left.is_opaque() && right.is_opaque(); let is_opaque = left.is_opaque() && right.is_opaque();
@ -251,7 +251,7 @@ impl ColorTest {
ui: &mut Ui, ui: &mut Ui,
tex_allocator: &mut Option<&mut dyn epi::TextureAllocator>, tex_allocator: &mut Option<&mut dyn epi::TextureAllocator>,
label: &str, label: &str,
bg_fill: Srgba, bg_fill: Color32,
gradient: &Gradient, gradient: &Gradient,
) { ) {
if !self.texture_gradients { if !self.texture_gradients {
@ -272,7 +272,7 @@ impl ColorTest {
} }
} }
fn vertex_gradient(&mut self, ui: &mut Ui, label: &str, bg_fill: Srgba, gradient: &Gradient) { fn vertex_gradient(&mut self, ui: &mut Ui, label: &str, bg_fill: Color32, gradient: &Gradient) {
if !self.vertex_gradients { if !self.vertex_gradients {
return; return;
} }
@ -286,7 +286,7 @@ impl ColorTest {
} }
} }
fn vertex_gradient(ui: &mut Ui, bg_fill: Srgba, gradient: &Gradient) -> Response { fn vertex_gradient(ui: &mut Ui, bg_fill: Color32, gradient: &Gradient) -> Response {
use egui::paint::*; use egui::paint::*;
let response = ui.allocate_response(GRADIENT_SIZE, Sense::hover()); let response = ui.allocate_response(GRADIENT_SIZE, Sense::hover());
if bg_fill != Default::default() { if bg_fill != Default::default() {
@ -315,16 +315,16 @@ fn vertex_gradient(ui: &mut Ui, bg_fill: Srgba, gradient: &Gradient) -> Response
} }
#[derive(Clone, Hash, PartialEq, Eq)] #[derive(Clone, Hash, PartialEq, Eq)]
struct Gradient(pub Vec<Srgba>); struct Gradient(pub Vec<Color32>);
impl Gradient { impl Gradient {
pub fn one_color(srgba: Srgba) -> Self { pub fn one_color(srgba: Color32) -> Self {
Self(vec![srgba, srgba]) Self(vec![srgba, srgba])
} }
pub fn texture_gradient(left: Srgba, right: Srgba) -> Self { pub fn texture_gradient(left: Color32, right: Color32) -> Self {
Self(vec![left, right]) Self(vec![left, right])
} }
pub fn ground_truth_linear_gradient(left: Srgba, right: Srgba) -> Self { pub fn ground_truth_linear_gradient(left: Color32, right: Color32) -> Self {
let left = Rgba::from(left); let left = Rgba::from(left);
let right = Rgba::from(right); let right = Rgba::from(right);
@ -333,19 +333,19 @@ impl Gradient {
(0..=n) (0..=n)
.map(|i| { .map(|i| {
let t = i as f32 / n as f32; let t = i as f32 / n as f32;
Srgba::from(lerp(left..=right, t)) Color32::from(lerp(left..=right, t))
}) })
.collect(), .collect(),
) )
} }
/// This is how a bad person blends `sRGBA` /// This is how a bad person blends `sRGBA`
pub fn ground_truth_bad_srgba_gradient(left: Srgba, right: Srgba) -> Self { pub fn ground_truth_bad_srgba_gradient(left: Color32, right: Color32) -> Self {
let n = 255; let n = 255;
Self( Self(
(0..=n) (0..=n)
.map(|i| { .map(|i| {
let t = i as f32 / n as f32; let t = i as f32 / n as f32;
Srgba::from_rgba_premultiplied( Color32::from_rgba_premultiplied(
lerp((left[0] as f32)..=(right[0] as f32), t).round() as u8, // Don't ever do this please! lerp((left[0] as f32)..=(right[0] as f32), t).round() as u8, // Don't ever do this please!
lerp((left[1] as f32)..=(right[1] as f32), t).round() as u8, // Don't ever do this please! lerp((left[1] as f32)..=(right[1] as f32), t).round() as u8, // Don't ever do this please!
lerp((left[2] as f32)..=(right[2] as f32), t).round() as u8, // Don't ever do this please! lerp((left[2] as f32)..=(right[2] as f32), t).round() as u8, // Don't ever do this please!
@ -357,20 +357,20 @@ impl Gradient {
} }
/// Do premultiplied alpha-aware blending of the gradient on top of the fill color /// Do premultiplied alpha-aware blending of the gradient on top of the fill color
pub fn with_bg_fill(self, bg: Srgba) -> Self { pub fn with_bg_fill(self, bg: Color32) -> Self {
let bg = Rgba::from(bg); let bg = Rgba::from(bg);
Self( Self(
self.0 self.0
.into_iter() .into_iter()
.map(|fg| { .map(|fg| {
let fg = Rgba::from(fg); let fg = Rgba::from(fg);
Srgba::from(bg * (1.0 - fg.a()) + fg) Color32::from(bg * (1.0 - fg.a()) + fg)
}) })
.collect(), .collect(),
) )
} }
pub fn to_pixel_row(&self) -> Vec<Srgba> { pub fn to_pixel_row(&self) -> Vec<Color32> {
self.0.clone() self.0.clone()
} }
} }

View file

@ -57,7 +57,7 @@ impl super::View for DancingStrings {
let thickness = 10.0 / mode; let thickness = 10.0 / mode;
cmds.push(paint::PaintCmd::line( cmds.push(paint::PaintCmd::line(
points, points,
Stroke::new(thickness, Srgba::additive_luminance(196)), Stroke::new(thickness, Color32::additive_luminance(196)),
)); ));
} }

View file

@ -93,7 +93,7 @@ impl DemoWindow {
let painter = ui.painter(); let painter = ui.painter();
let c = response.rect.center(); let c = response.rect.center();
let r = response.rect.width() / 2.0 - 1.0; let r = response.rect.width() / 2.0 - 1.0;
let color = Srgba::gray(128); let color = Color32::gray(128);
let stroke = Stroke::new(1.0, color); let stroke = Stroke::new(1.0, color);
painter.circle_stroke(c, r, stroke); painter.circle_stroke(c, r, stroke);
painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke); painter.line_segment([c - vec2(0.0, r), c + vec2(0.0, r)], stroke);
@ -212,7 +212,7 @@ impl BoxPainting {
ui.painter().rect( ui.painter().rect(
response.rect, response.rect,
self.corner_radius, self.corner_radius,
Srgba::gray(64), Color32::gray(64),
Stroke::new(self.stroke_width, WHITE), Stroke::new(self.stroke_width, WHITE),
); );
} }

View file

@ -21,7 +21,7 @@ pub struct Widgets {
radio: Enum, radio: Enum,
sliders: super::Sliders, sliders: super::Sliders,
angle: f32, angle: f32,
color: Srgba, color: Color32,
single_line_text_input: String, single_line_text_input: String,
multiline_text_input: String, multiline_text_input: String,
toggle_switch: bool, toggle_switch: bool,

View file

@ -129,7 +129,7 @@ impl FractalClock {
]; ];
let scale = self.zoom * rect.width().min(rect.height()); let scale = self.zoom * rect.width().min(rect.height());
let paint_line = |points: [Pos2; 2], color: Srgba, width: f32| { let paint_line = |points: [Pos2; 2], color: Color32, width: f32| {
let line = [ let line = [
rect.center() + scale * points[0].to_vec2(), rect.center() + scale * points[0].to_vec2(),
rect.center() + scale * points[1].to_vec2(), rect.center() + scale * points[1].to_vec2(),
@ -161,7 +161,7 @@ impl FractalClock {
for (i, hand) in hands.iter().enumerate() { for (i, hand) in hands.iter().enumerate() {
let center = pos2(0.0, 0.0); let center = pos2(0.0, 0.0);
let end = center + hand.vec; let end = center + hand.vec;
paint_line([center, end], Srgba::additive_luminance(255), width); paint_line([center, end], Color32::additive_luminance(255), width);
if i < 2 { if i < 2 {
nodes.push(Node { nodes.push(Node {
pos: end, pos: end,
@ -191,7 +191,7 @@ impl FractalClock {
}; };
paint_line( paint_line(
[a.pos, b.pos], [a.pos, b.pos],
Srgba::additive_luminance(luminance_u8), Color32::additive_luminance(luminance_u8),
width, width,
); );
new_nodes.push(b); new_nodes.push(b);

View file

@ -239,7 +239,7 @@ impl ColoredText {
ui.style_mut().spacing.item_spacing.x = 0.0; ui.style_mut().spacing.item_spacing.x = 0.0;
for (style, range) in line { for (style, range) in line {
let fg = style.foreground; let fg = style.foreground;
let text_color = egui::Srgba::from_rgb(fg.r, fg.g, fg.b); let text_color = egui::Color32::from_rgb(fg.r, fg.g, fg.b);
ui.add(egui::Label::new(range).monospace().text_color(text_color)); ui.add(egui::Label::new(range).monospace().text_color(text_color));
} }
}); });
@ -277,7 +277,7 @@ impl TexMngr {
struct Image { struct Image {
size: (usize, usize), size: (usize, usize),
pixels: Vec<egui::Srgba>, pixels: Vec<egui::Color32>,
} }
impl Image { impl Image {
@ -290,7 +290,7 @@ impl Image {
assert_eq!(size.0 * size.1 * 4, pixels.len()); assert_eq!(size.0 * size.1 * 4, pixels.len());
let pixels = pixels let pixels = pixels
.chunks(4) .chunks(4)
.map(|p| egui::Srgba::from_rgba_unmultiplied(p[0], p[1], p[2], p[3])) .map(|p| egui::Color32::from_rgba_unmultiplied(p[0], p[1], p[2], p[3]))
.collect(); .collect();
Some(Image { size, pixels }) Some(Image { size, pixels })

View file

@ -78,7 +78,7 @@ impl FrameHistory {
}]; }];
let rect = rect.shrink(4.0); let rect = rect.shrink(4.0);
let line_stroke = Stroke::new(1.0, Srgba::additive_luminance(128)); let line_stroke = Stroke::new(1.0, Color32::additive_luminance(128));
if let Some(mouse_pos) = ui.input().mouse.pos { if let Some(mouse_pos) = ui.input().mouse.pos {
if rect.contains(mouse_pos) { if rect.contains(mouse_pos) {
@ -100,7 +100,7 @@ impl FrameHistory {
} }
} }
let circle_color = Srgba::additive_luminance(196); let circle_color = Color32::additive_luminance(196);
let radius = 2.0; let radius = 2.0;
let right_side_time = ui.input().time; // Time at right side of screen let right_side_time = ui.input().time; // Time at right side of screen

View file

@ -2,7 +2,7 @@ use std::time::Instant;
use crate::{storage::WindowSettings, *}; use crate::{storage::WindowSettings, *};
pub use egui::Srgba; pub use egui::Color32;
const EGUI_MEMORY_KEY: &str = "egui"; const EGUI_MEMORY_KEY: &str = "egui";
const WINDOW_KEY: &str = "window"; const WINDOW_KEY: &str = "window";
@ -16,7 +16,7 @@ impl epi::TextureAllocator for Painter {
&mut self, &mut self,
id: egui::TextureId, id: egui::TextureId,
size: (usize, usize), size: (usize, usize),
srgba_pixels: &[Srgba], srgba_pixels: &[Color32],
) { ) {
self.set_user_texture(id, size, srgba_pixels); self.set_user_texture(id, size, srgba_pixels);
} }

View file

@ -4,7 +4,7 @@ use {
egui::{ egui::{
math::clamp, math::clamp,
paint::{PaintJobs, Triangles}, paint::{PaintJobs, Triangles},
Rect, Srgba, Color32, Rect,
}, },
glium::{ glium::{
implement_vertex, implement_vertex,
@ -109,7 +109,7 @@ impl Painter {
.chunks(texture.width as usize) .chunks(texture.width as usize)
.map(|row| { .map(|row| {
row.iter() row.iter()
.map(|&a| Srgba::white_alpha(a).to_tuple()) .map(|&a| Color32::white_alpha(a).to_tuple())
.collect() .collect()
}) })
.collect(); .collect();
@ -282,7 +282,7 @@ impl Painter {
&mut self, &mut self,
id: egui::TextureId, id: egui::TextureId,
size: (usize, usize), size: (usize, usize),
pixels: &[Srgba], pixels: &[Color32],
) { ) {
assert_eq!(size.0 * size.1, pixels.len()); assert_eq!(size.0 * size.1, pixels.len());

View file

@ -1,6 +1,6 @@
use crate::*; use crate::*;
pub use egui::{pos2, Srgba}; pub use egui::{pos2, Color32};
use http::WebHttp; use http::WebHttp;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -75,7 +75,7 @@ impl epi::TextureAllocator for webgl::Painter {
&mut self, &mut self,
id: egui::TextureId, id: egui::TextureId,
size: (usize, usize), size: (usize, usize),
srgba_pixels: &[Srgba], srgba_pixels: &[Color32],
) { ) {
self.set_user_texture(id, size, srgba_pixels); self.set_user_texture(id, size, srgba_pixels);
} }

View file

@ -6,7 +6,7 @@ use {
use egui::{ use egui::{
math::clamp, math::clamp,
paint::{PaintJobs, Srgba, Texture, Triangles}, paint::{Color32, PaintJobs, Texture, Triangles},
vec2, vec2,
}; };
@ -182,7 +182,7 @@ impl Painter {
&mut self, &mut self,
id: egui::TextureId, id: egui::TextureId,
size: (usize, usize), size: (usize, usize),
srgba_pixels: &[Srgba], srgba_pixels: &[Color32],
) { ) {
assert_eq!(size.0 * size.1, srgba_pixels.len()); assert_eq!(size.0 * size.1, srgba_pixels.len());
@ -328,7 +328,7 @@ impl Painter {
self.canvas.width() as i32, self.canvas.width() as i32,
self.canvas.height() as i32, self.canvas.height() as i32,
); );
let clear_color: Srgba = clear_color.into(); let clear_color: Color32 = clear_color.into();
gl.clear_color( gl.clear_color(
clear_color[0] as f32 / 255.0, clear_color[0] as f32 / 255.0,
clear_color[1] as f32 / 255.0, clear_color[1] as f32 / 255.0,

View file

@ -101,7 +101,7 @@ pub trait App {
/// This is the background of your windows if you don't set a central panel. /// This is the background of your windows if you don't set a central panel.
fn clear_color(&self) -> egui::Rgba { fn clear_color(&self) -> egui::Rgba {
// NOTE: a bright gray makes the shadows of the windows look weird. // NOTE: a bright gray makes the shadows of the windows look weird.
egui::Srgba::from_rgb(12, 12, 12).into() egui::Color32::from_rgb(12, 12, 12).into()
} }
} }
@ -196,7 +196,7 @@ pub trait TextureAllocator {
&mut self, &mut self,
id: egui::TextureId, id: egui::TextureId,
size: (usize, usize), size: (usize, usize),
srgba_pixels: &[egui::Srgba], srgba_pixels: &[egui::Color32],
); );
/// Free the given texture. /// Free the given texture.