Move Color32 constants from mod color to struct Color32

This commit is contained in:
Emil Ernerfeldt 2021-01-02 17:18:41 +01:00
parent 64dd186daf
commit 029a85c1fc
20 changed files with 77 additions and 62 deletions

View file

@ -305,12 +305,12 @@ impl Resize {
if ui.ctx().style().visuals.debug_resize { if ui.ctx().style().visuals.debug_resize {
ui.ctx().debug_painter().debug_rect( ui.ctx().debug_painter().debug_rect(
Rect::from_min_size(content_ui.min_rect().left_top(), state.desired_size), Rect::from_min_size(content_ui.min_rect().left_top(), state.desired_size),
color::GREEN, Color32::GREEN,
"desired_size", "desired_size",
); );
ui.ctx().debug_painter().debug_rect( ui.ctx().debug_painter().debug_rect(
Rect::from_min_size(content_ui.min_rect().left_top(), state.last_content_size), Rect::from_min_size(content_ui.min_rect().left_top(), state.last_content_size),
color::LIGHT_BLUE, Color32::LIGHT_BLUE,
"last_content_size", "last_content_size",
); );
} }

View file

@ -816,7 +816,7 @@ impl Context {
{ {
ui.ctx() ui.ctx()
.debug_painter() .debug_painter()
.debug_rect(area.rect(), color::RED, ""); .debug_rect(area.rect(), Color32::RED, "");
} }
} }
} }

View file

@ -1,7 +1,7 @@
//! uis for egui types. //! uis for egui types.
use crate::{ use crate::{
math::*, math::*,
paint::{self, color::WHITE, PaintCmd, Texture, Triangles}, paint::{self, PaintCmd, Texture, Triangles},
*, *,
}; };
@ -21,7 +21,11 @@ impl Texture {
let response = ui.allocate_response(size, Sense::hover()); let response = ui.allocate_response(size, Sense::hover());
let rect = response.rect; let rect = response.rect;
let mut triangles = Triangles::default(); let mut triangles = Triangles::default();
triangles.add_rect_with_uv(rect, [pos2(0.0, 0.0), pos2(1.0, 1.0)].into(), WHITE); triangles.add_rect_with_uv(
rect,
[pos2(0.0, 0.0), pos2(1.0, 1.0)].into(),
Color32::WHITE,
);
ui.painter().add(PaintCmd::triangles(triangles)); ui.painter().add(PaintCmd::triangles(triangles));
let (tex_w, tex_h) = (self.width as f32, self.height as f32); let (tex_w, tex_h) = (self.width as f32, self.height as f32);
@ -45,7 +49,7 @@ impl Texture {
pos2((u + texel_radius) / tex_w, (v + texel_radius) / tex_h), pos2((u + texel_radius) / tex_w, (v + texel_radius) / tex_h),
); );
let mut triangles = Triangles::default(); let mut triangles = Triangles::default();
triangles.add_rect_with_uv(zoom_rect, uv_rect, WHITE); triangles.add_rect_with_uv(zoom_rect, uv_rect, Color32::WHITE);
ui.painter().add(PaintCmd::triangles(triangles)); ui.painter().add(PaintCmd::triangles(triangles));
}); });
} }

View file

@ -501,7 +501,7 @@ impl Layout {
let cursor = region.cursor; let cursor = region.cursor;
let color = color::GREEN; let color = Color32::GREEN;
let stroke = Stroke::new(2.0, color); let stroke = Stroke::new(2.0, color);
let align; let align;

View file

@ -138,7 +138,7 @@ pub fn warn_if_debug_build(ui: &mut crate::Ui) {
ui.label( ui.label(
crate::Label::new("‼ Debug build ‼") crate::Label::new("‼ Debug build ‼")
.small() .small()
.text_color(crate::color::RED), .text_color(crate::Color32::RED),
) )
.on_hover_text("Egui was compiled with debug assertions enabled."); .on_hover_text("Egui was compiled with debug assertions enabled.");
} }

View file

@ -15,7 +15,7 @@
//! } //! }
//! ``` //! ```
use crate::{color::TRANSPARENT, paint::Stroke, widgets::*, *}; use crate::{paint::Stroke, widgets::*, *};
/// What is saved between frames. /// What is saved between frames.
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug, Default)]
@ -44,11 +44,11 @@ pub fn bar<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Respo
ui.horizontal(|ui| { ui.horizontal(|ui| {
let mut style = ui.style().clone(); let mut style = ui.style().clone();
style.spacing.button_padding = vec2(2.0, 0.0); style.spacing.button_padding = vec2(2.0, 0.0);
// style.visuals.widgets.active.bg_fill = TRANSPARENT; // style.visuals.widgets.active.bg_fill = Color32::TRANSPARENT;
style.visuals.widgets.active.bg_stroke = Stroke::none(); style.visuals.widgets.active.bg_stroke = Stroke::none();
// style.visuals.widgets.hovered.bg_fill = TRANSPARENT; // style.visuals.widgets.hovered.bg_fill = Color32::TRANSPARENT;
style.visuals.widgets.hovered.bg_stroke = Stroke::none(); style.visuals.widgets.hovered.bg_stroke = Stroke::none();
style.visuals.widgets.inactive.bg_fill = TRANSPARENT; style.visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT;
style.visuals.widgets.inactive.bg_stroke = Stroke::none(); style.visuals.widgets.inactive.bg_stroke = Stroke::none();
ui.set_style(style); ui.set_style(style);
@ -104,11 +104,11 @@ fn menu_impl<'c>(
frame.show(ui, |ui| { frame.show(ui, |ui| {
let mut style = ui.style().clone(); let mut style = ui.style().clone();
style.spacing.button_padding = vec2(2.0, 0.0); style.spacing.button_padding = vec2(2.0, 0.0);
// style.visuals.widgets.active.bg_fill = TRANSPARENT; // style.visuals.widgets.active.bg_fill = Color32::TRANSPARENT;
style.visuals.widgets.active.bg_stroke = Stroke::none(); style.visuals.widgets.active.bg_stroke = Stroke::none();
// style.visuals.widgets.hovered.bg_fill = TRANSPARENT; // style.visuals.widgets.hovered.bg_fill = Color32::TRANSPARENT;
style.visuals.widgets.hovered.bg_stroke = Stroke::none(); style.visuals.widgets.hovered.bg_stroke = Stroke::none();
style.visuals.widgets.inactive.bg_fill = TRANSPARENT; style.visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT;
style.visuals.widgets.inactive.bg_stroke = Stroke::none(); style.visuals.widgets.inactive.bg_stroke = Stroke::none();
ui.set_style(style); ui.set_style(style);
ui.with_layout(Layout::top_down_justified(Align::left()), add_contents); ui.with_layout(Layout::top_down_justified(Align::left()), add_contents);

View file

@ -24,17 +24,22 @@ impl std::ops::IndexMut<usize> for Color32 {
} }
} }
// TODO: remove ?
#[deprecated = "Replaced by Color32::from_rgb… family of functions."] #[deprecated = "Replaced by Color32::from_rgb… family of functions."]
pub const fn srgba(r: u8, g: u8, b: u8, a: u8) -> Color32 { pub const fn srgba(r: u8, g: u8, b: u8, a: u8) -> Color32 {
Color32::from_rgba_premultiplied(r, g, b, a) Color32::from_rgba_premultiplied(r, g, b, a)
} }
impl Color32 { impl Color32 {
#[deprecated = "Use from_rgb(..), from_rgba_premultiplied(..) or from_srgba_unmultiplied(..)"] pub const TRANSPARENT: Color32 = Color32::from_rgba_premultiplied(0, 0, 0, 0);
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self { pub const BLACK: Color32 = Color32::from_rgb(0, 0, 0);
Self([r, g, b, a]) pub const LIGHT_GRAY: Color32 = Color32::from_rgb(220, 220, 220);
} pub const GRAY: Color32 = Color32::from_rgb(160, 160, 160);
pub const WHITE: Color32 = Color32::from_rgb(255, 255, 255);
pub const RED: Color32 = Color32::from_rgb(255, 0, 0);
pub const GREEN: Color32 = Color32::from_rgb(0, 255, 0);
pub const BLUE: Color32 = Color32::from_rgb(0, 0, 255);
pub const YELLOW: Color32 = Color32::from_rgb(255, 255, 0);
pub const LIGHT_BLUE: Color32 = Color32::from_rgb(140, 160, 255);
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self { pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self([r, g, b, 255]) Self([r, g, b, 255])
@ -60,6 +65,11 @@ impl Color32 {
} }
} }
#[deprecated = "Use from_rgb(..), from_rgba_premultiplied(..) or from_srgba_unmultiplied(..)"]
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self([r, g, b, a])
}
pub const fn gray(l: u8) -> Self { pub const fn gray(l: u8) -> Self {
Self([l, l, l, 255]) Self([l, l, l, 255])
} }
@ -109,19 +119,6 @@ impl Color32 {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
pub const TRANSPARENT: Color32 = Color32::from_rgba_premultiplied(0, 0, 0, 0);
pub const BLACK: Color32 = Color32::from_rgb(0, 0, 0);
pub const LIGHT_GRAY: Color32 = Color32::from_rgb(220, 220, 220);
pub const GRAY: Color32 = Color32::from_rgb(160, 160, 160);
pub const WHITE: Color32 = Color32::from_rgb(255, 255, 255);
pub const RED: Color32 = Color32::from_rgb(255, 0, 0);
pub const GREEN: Color32 = Color32::from_rgb(0, 255, 0);
pub const BLUE: Color32 = Color32::from_rgb(0, 0, 255);
pub const YELLOW: Color32 = Color32::from_rgb(255, 255, 0);
pub const LIGHT_BLUE: Color32 = Color32::from_rgb(140, 160, 255);
// ----------------------------------------------------------------------------
/// 0-1 linear space `RGBA` color with premultiplied alpha. /// 0-1 linear space `RGBA` color with premultiplied alpha.
#[derive(Clone, Copy, Debug, Default, PartialEq)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]

View file

@ -204,7 +204,7 @@ pub struct Stroke {
impl Stroke { impl Stroke {
pub fn none() -> Self { pub fn none() -> Self {
Self::new(0.0, crate::color::TRANSPARENT) Self::new(0.0, Color32::TRANSPARENT)
} }
pub fn new(width: impl Into<f32>, color: impl Into<Color32>) -> Self { pub fn new(width: impl Into<f32>, color: impl Into<Color32>) -> Self {

View file

@ -7,7 +7,7 @@
use { use {
super::{ super::{
color::{self, Color32, Rgba, TRANSPARENT}, color::{Color32, Rgba},
*, *,
}, },
crate::math::*, crate::math::*,
@ -479,7 +479,7 @@ fn fill_closed_path(
options: TessellationOptions, options: TessellationOptions,
out: &mut Triangles, out: &mut Triangles,
) { ) {
if color == color::TRANSPARENT { if color == Color32::TRANSPARENT {
return; return;
} }
@ -487,7 +487,7 @@ fn fill_closed_path(
if options.anti_alias { if options.anti_alias {
out.reserve_triangles(3 * n as usize); out.reserve_triangles(3 * n as usize);
out.reserve_vertices(2 * n as usize); out.reserve_vertices(2 * n as usize);
let color_outer = color::TRANSPARENT; let color_outer = Color32::TRANSPARENT;
let idx_inner = out.vertices.len() as u32; let idx_inner = out.vertices.len() as u32;
let idx_outer = idx_inner + 1; let idx_outer = idx_inner + 1;
for i in 2..n { for i in 2..n {
@ -525,7 +525,7 @@ fn stroke_path(
options: TessellationOptions, options: TessellationOptions,
out: &mut Triangles, out: &mut Triangles,
) { ) {
if stroke.width <= 0.0 || stroke.color == color::TRANSPARENT { if stroke.width <= 0.0 || stroke.color == Color32::TRANSPARENT {
return; return;
} }
@ -534,7 +534,7 @@ fn stroke_path(
if options.anti_alias { if options.anti_alias {
let color_inner = stroke.color; let color_inner = stroke.color;
let color_outer = color::TRANSPARENT; let color_outer = Color32::TRANSPARENT;
let thin_line = stroke.width <= options.aa_size; let thin_line = stroke.width <= options.aa_size;
if thin_line { if thin_line {
@ -547,7 +547,7 @@ fn stroke_path(
// Fade out as it gets thinner: // Fade out as it gets thinner:
let color_inner = mul_color(color_inner, stroke.width / options.aa_size); let color_inner = mul_color(color_inner, stroke.width / options.aa_size);
if color_inner == color::TRANSPARENT { if color_inner == Color32::TRANSPARENT {
return; return;
} }
@ -639,7 +639,7 @@ fn stroke_path(
// Fade out thin lines rather than making them thinner // Fade out thin lines rather than making them thinner
let radius = options.aa_size / 2.0; let radius = options.aa_size / 2.0;
let color = mul_color(stroke.color, stroke.width / options.aa_size); let color = mul_color(stroke.color, stroke.width / options.aa_size);
if color == color::TRANSPARENT { if color == Color32::TRANSPARENT {
return; return;
} }
for p in path { for p in path {
@ -756,7 +756,7 @@ impl Tessellator {
path.add_open_points(&points); path.add_open_points(&points);
} }
if fill != TRANSPARENT { if fill != Color32::TRANSPARENT {
debug_assert!( debug_assert!(
closed, closed,
"You asked to fill a path that is not closed. That makes no sense." "You asked to fill a path that is not closed. That makes no sense."
@ -831,7 +831,7 @@ impl Tessellator {
color: Color32, color: Color32,
out: &mut Triangles, out: &mut Triangles,
) { ) {
if color == TRANSPARENT { if color == Color32::TRANSPARENT {
return; return;
} }
galley.sanity_check(); galley.sanity_check();

View file

@ -1,6 +1,5 @@
use crate::{ use crate::{
align::{anchor_rect, Align, LEFT_TOP}, align::{anchor_rect, Align, LEFT_TOP},
color,
layers::PaintCmdIdx, layers::PaintCmdIdx,
math::{Pos2, Rect, Vec2}, math::{Pos2, Rect, Vec2},
paint::{Fonts, Galley, PaintCmd, Stroke, TextStyle}, paint::{Fonts, Galley, PaintCmd, Stroke, TextStyle},
@ -149,9 +148,9 @@ impl Painter {
rect: frame_rect, rect: frame_rect,
corner_radius: 0.0, corner_radius: 0.0,
fill: Color32::black_alpha(240), fill: Color32::black_alpha(240),
stroke: Stroke::new(1.0, color::RED), stroke: Stroke::new(1.0, Color32::RED),
}); });
self.galley(rect.min, galley, text_style, color::RED); self.galley(rect.min, galley, text_style, Color32::RED);
frame_rect frame_rect
} }
} }

View file

@ -301,10 +301,10 @@ impl Default for Widgets {
Self { Self {
active: WidgetVisuals { active: WidgetVisuals {
bg_fill: Rgba::luminance_alpha(0.10, 0.5).into(), bg_fill: Rgba::luminance_alpha(0.10, 0.5).into(),
bg_stroke: Stroke::new(2.0, WHITE), bg_stroke: Stroke::new(2.0, Color32::WHITE),
corner_radius: 4.0, corner_radius: 4.0,
fg_fill: Color32::from_rgb(120, 120, 200), fg_fill: Color32::from_rgb(120, 120, 200),
fg_stroke: Stroke::new(2.0, WHITE), fg_stroke: Stroke::new(2.0, Color32::WHITE),
}, },
hovered: WidgetVisuals { hovered: WidgetVisuals {
bg_fill: Rgba::luminance_alpha(0.06, 0.5).into(), bg_fill: Rgba::luminance_alpha(0.06, 0.5).into(),

View file

@ -461,7 +461,7 @@ impl Ui {
/// # let mut ui = egui::Ui::__test(); /// # let mut ui = egui::Ui::__test();
/// let response = ui.allocate_response(egui::vec2(100.0, 200.0), egui::Sense::click()); /// let response = ui.allocate_response(egui::vec2(100.0, 200.0), egui::Sense::click());
/// if response.clicked { /* … */ } /// if response.clicked { /* … */ }
/// ui.painter().rect_stroke(response.rect, 0.0, (1.0, egui::color::WHITE)); /// ui.painter().rect_stroke(response.rect, 0.0, (1.0, egui::Color32::WHITE));
/// ``` /// ```
pub fn allocate_response(&mut self, desired_size: Vec2, sense: Sense) -> Response { pub fn allocate_response(&mut self, desired_size: Vec2, sense: Sense) -> Response {
let (id, rect) = self.allocate_space(desired_size); let (id, rect) = self.allocate_space(desired_size);
@ -500,7 +500,8 @@ impl Ui {
let debug_expand_height = self.style().visuals.debug_expand_height; let debug_expand_height = self.style().visuals.debug_expand_height;
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, Color32::LIGHT_BLUE));
let color = color::Color32::from_rgb(200, 0, 0); let color = color::Color32::from_rgb(200, 0, 0);
let width = 2.5; let width = 2.5;

View file

@ -7,9 +7,9 @@ use crate::{
fn contrast_color(color: impl Into<Rgba>) -> Color32 { fn contrast_color(color: impl Into<Rgba>) -> Color32 {
if color.into().intensity() < 0.5 { if color.into().intensity() < 0.5 {
color::WHITE Color32::WHITE
} else { } else {
color::BLACK Color32::BLACK
} }
} }

View file

@ -18,7 +18,7 @@ impl Image {
uv: Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), uv: Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)),
desired_size: desired_size.into(), desired_size: desired_size.into(),
bg_fill: Default::default(), bg_fill: Default::default(),
tint: color::WHITE, tint: Color32::WHITE,
} }
} }

View file

@ -3,6 +3,12 @@ use std::collections::HashMap;
const GRADIENT_SIZE: Vec2 = vec2(256.0, 24.0); const GRADIENT_SIZE: Vec2 = vec2(256.0, 24.0);
const BLACK: Color32 = Color32::BLACK;
const GREEN: Color32 = Color32::GREEN;
const RED: Color32 = Color32::RED;
const TRANSPARENT: Color32 = Color32::TRANSPARENT;
const WHITE: Color32 = Color32::WHITE;
#[derive(serde::Deserialize, serde::Serialize)] #[derive(serde::Deserialize, serde::Serialize)]
pub struct ColorTest { pub struct ColorTest {
#[serde(skip)] #[serde(skip)]
@ -32,7 +38,7 @@ impl epi::App for ColorTest {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
if frame.is_web() { if frame.is_web() {
ui.colored_label( ui.colored_label(
egui::color::RED, RED,
"NOTE: The current WebGL backend does NOT pass the color test!", "NOTE: The current WebGL backend does NOT pass the color test!",
); );
ui.separator(); ui.separator();

View file

@ -213,7 +213,7 @@ impl BoxPainting {
response.rect, response.rect,
self.corner_radius, self.corner_radius,
Color32::gray(64), Color32::gray(64),
Stroke::new(self.stroke_width, WHITE), Stroke::new(self.stroke_width, Color32::WHITE),
); );
} }
}); });
@ -388,7 +388,11 @@ impl Tree {
} }
fn children_ui(&mut self, ui: &mut Ui, depth: usize) -> Action { fn children_ui(&mut self, ui: &mut Ui, depth: usize) -> Action {
if depth > 0 && ui.add(Button::new("delete").text_color(color::RED)).clicked { if depth > 0
&& ui
.add(Button::new("delete").text_color(Color32::RED))
.clicked
{
return Action::Delete; return Action::Delete;
} }

View file

@ -11,7 +11,7 @@ impl Default for Painting {
fn default() -> Self { fn default() -> Self {
Self { Self {
lines: Default::default(), lines: Default::default(),
stroke: Stroke::new(1.0, color::LIGHT_BLUE), stroke: Stroke::new(1.0, Color32::LIGHT_BLUE),
} }
} }
} }

View file

@ -48,7 +48,7 @@ impl Scrolls {
const ALIGNS: [Align; 3] = [Align::Min, Align::Center, Align::Max]; const ALIGNS: [Align; 3] = [Align::Min, Align::Center, Align::Max];
ui.columns(3, |cols| { ui.columns(3, |cols| {
for (i, col) in cols.iter_mut().enumerate() { for (i, col) in cols.iter_mut().enumerate() {
col.colored_label(WHITE, TITLES[i]); col.colored_label(Color32::WHITE, TITLES[i]);
let mut scroll_area = ScrollArea::from_max_height(200.0).id_source(i); let mut scroll_area = ScrollArea::from_max_height(200.0).id_source(i);
if scroll_offset { if scroll_offset {
self.tracking = false; self.tracking = false;
@ -62,7 +62,8 @@ impl Scrolls {
ui.vertical(|ui| { ui.vertical(|ui| {
for item in 1..=50 { for item in 1..=50 {
if self.tracking && item == self.track_item { if self.tracking && item == self.track_item {
let response = ui.colored_label(YELLOW, format!("Item {}", item)); let response =
ui.colored_label(Color32::YELLOW, format!("Item {}", item));
response.scroll_to_me(ALIGNS[i]); response.scroll_to_me(ALIGNS[i]);
} else { } else {
ui.label(format!("Item {}", item)); ui.label(format!("Item {}", item));
@ -80,7 +81,10 @@ impl Scrolls {
ui.min_rect().height() - ui.clip_rect().height() + 2.0 * margin, ui.min_rect().height() - ui.clip_rect().height() + 2.0 * margin,
) )
}); });
col.colored_label(WHITE, format!("{:.0}/{:.0}", current_scroll, max_scroll)); col.colored_label(
Color32::WHITE,
format!("{:.0}/{:.0}", current_scroll, max_scroll),
);
} }
}); });
} }

View file

@ -100,7 +100,7 @@ impl epi::App for HttpApp {
} }
Err(error) => { Err(error) => {
// This should only happen if the fetch API isn't available or something similar. // This should only happen if the fetch API isn't available or something similar.
ui.add(egui::Label::new(error).text_color(egui::color::RED)); ui.add(egui::Label::new(error).text_color(egui::Color32::RED));
} }
} }
} }

View file

@ -95,7 +95,7 @@ impl FrameHistory {
align::LEFT_BOTTOM, align::LEFT_BOTTOM,
text, text,
TextStyle::Monospace, TextStyle::Monospace,
color::WHITE, Color32::WHITE,
)); ));
} }
} }