From 01f8c4336b4325b6d6926150c000bfa162aceb47 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 12 Dec 2022 15:48:59 +0100 Subject: [PATCH 1/9] Revert "fix all clippy lints and remove them from allow list in cranky (#2419)" This reverts commit 930ef2db3817cc7104c5f6ab81dcc9ddb219a1d7. --- Cranky.toml | 5 +++++ crates/ecolor/src/color32.rs | 2 +- crates/ecolor/src/lib.rs | 2 +- crates/ecolor/src/rgba.rs | 8 ++++---- crates/egui_demo_app/src/apps/custom3d_glow.rs | 11 ++++++----- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Cranky.toml b/Cranky.toml index 9e829f6b518..957bd477aa4 100644 --- a/Cranky.toml +++ b/Cranky.toml @@ -115,4 +115,9 @@ warn = [ allow = [ # TODO(emilk): enable more lints "clippy::type_complexity", + "clippy::undocumented_unsafe_blocks", + "clippy::manual_range_contains", + "trivial_casts", + "unsafe_op_in_unsafe_fn", # `unsafe_op_in_unsafe_fn` may become the default in future Rust versions: https://github.com/rust-lang/rust/issues/71668 + "unused_qualifications", ] diff --git a/crates/ecolor/src/color32.rs b/crates/ecolor/src/color32.rs index 9842001c05d..ee22c4d047c 100644 --- a/crates/ecolor/src/color32.rs +++ b/crates/ecolor/src/color32.rs @@ -173,7 +173,7 @@ impl Color32 { /// Multiply with 0.5 to make color half as opaque. pub fn linear_multiply(self, factor: f32) -> Color32 { - crate::ecolor_assert!((0.0..=1.0).contains(&factor)); + crate::ecolor_assert!(0.0 <= factor && factor <= 1.0); // As an unfortunate side-effect of using premultiplied alpha // we need a somewhat expensive conversion to linear space and back. Rgba::from(self).multiply(factor).into() diff --git a/crates/ecolor/src/lib.rs b/crates/ecolor/src/lib.rs index 82735054dc7..9bc42c4cf79 100644 --- a/crates/ecolor/src/lib.rs +++ b/crates/ecolor/src/lib.rs @@ -100,7 +100,7 @@ fn fast_round(r: f32) -> u8 { pub fn test_srgba_conversion() { for b in 0..=255 { let l = linear_f32_from_gamma_u8(b); - assert!((0.0..=1.0).contains(&l)); + assert!(0.0 <= l && l <= 1.0); assert_eq!(gamma_u8_from_linear_f32(l), b); } } diff --git a/crates/ecolor/src/rgba.rs b/crates/ecolor/src/rgba.rs index 6584e3af1f3..f9d671fd5c1 100644 --- a/crates/ecolor/src/rgba.rs +++ b/crates/ecolor/src/rgba.rs @@ -96,22 +96,22 @@ impl Rgba { } pub fn from_luminance_alpha(l: f32, a: f32) -> Self { - crate::ecolor_assert!((0.0..=1.0).contains(&l)); - crate::ecolor_assert!((0.0..=1.0).contains(&a)); + crate::ecolor_assert!(0.0 <= l && l <= 1.0); + crate::ecolor_assert!(0.0 <= a && a <= 1.0); Self([l * a, l * a, l * a, a]) } /// Transparent black #[inline(always)] pub fn from_black_alpha(a: f32) -> Self { - crate::ecolor_assert!((0.0..=1.0).contains(&a)); + crate::ecolor_assert!(0.0 <= a && a <= 1.0); Self([0.0, 0.0, 0.0, a]) } /// Transparent white #[inline(always)] pub fn from_white_alpha(a: f32) -> Self { - crate::ecolor_assert!((0.0..=1.0).contains(&a), "a: {}", a); + crate::ecolor_assert!(0.0 <= a && a <= 1.0, "a: {}", a); Self([a, a, a, a]) } diff --git a/crates/egui_demo_app/src/apps/custom3d_glow.rs b/crates/egui_demo_app/src/apps/custom3d_glow.rs index d5b4f2603aa..5d82d75c244 100644 --- a/crates/egui_demo_app/src/apps/custom3d_glow.rs +++ b/crates/egui_demo_app/src/apps/custom3d_glow.rs @@ -146,11 +146,12 @@ impl RotatingTriangle { ), ); gl.compile_shader(shader); - assert!( - gl.get_shader_compile_status(shader), - "Failed to compile custom_3d_glow: {}", - gl.get_shader_info_log(shader) - ); + if !gl.get_shader_compile_status(shader) { + panic!( + "Failed to compile custom_3d_glow: {}", + gl.get_shader_info_log(shader) + ); + } gl.attach_shader(program, shader); shader }) From d56bb127238e92c550eb8e7f15bf2ce07f1f4cd0 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 12 Dec 2022 15:50:03 +0100 Subject: [PATCH 2/9] Explain the cranky lints better --- Cranky.toml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Cranky.toml b/Cranky.toml index 957bd477aa4..6fa6ca093ea 100644 --- a/Cranky.toml +++ b/Cranky.toml @@ -113,10 +113,11 @@ warn = [ ] allow = [ - # TODO(emilk): enable more lints + "clippy::manual_range_contains", # This one is just annoying + + # Some of these we should try to put in "warn": "clippy::type_complexity", "clippy::undocumented_unsafe_blocks", - "clippy::manual_range_contains", "trivial_casts", "unsafe_op_in_unsafe_fn", # `unsafe_op_in_unsafe_fn` may become the default in future Rust versions: https://github.com/rust-lang/rust/issues/71668 "unused_qualifications", From 270e0ff3b1ee5bd4534714d6bd048d5346ff71e6 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 12 Dec 2022 15:50:18 +0100 Subject: [PATCH 3/9] Add Color32::gamma_multiply --- crates/ecolor/src/color32.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/ecolor/src/color32.rs b/crates/ecolor/src/color32.rs index ee22c4d047c..93962642517 100644 --- a/crates/ecolor/src/color32.rs +++ b/crates/ecolor/src/color32.rs @@ -171,7 +171,27 @@ impl Color32 { Rgba::from(*self).to_srgba_unmultiplied() } - /// Multiply with 0.5 to make color half as opaque. + /// Multiply with 0.5 to make color half as opaque, perceptually. + /// + /// Fast multiplication in gamma-space. + /// + /// This is perceptually even, and faster that [`Self::linear_multiply`]. + #[inline] + pub fn gamma_multiply(self, factor: f32) -> Color32 { + crate::ecolor_assert!(0.0 <= factor && factor <= 1.0); + let Self([r, g, b, a]) = self; + Self([ + (r as f32 * factor + 0.5) as u8, + (g as f32 * factor + 0.5) as u8, + (b as f32 * factor + 0.5) as u8, + (a as f32 * factor + 0.5) as u8, + ]) + } + + /// Multiply with 0.5 to make color half as opaque in linear space. + /// + /// This is using linear space, which is not perceptually even. + /// You may want to use [`Self::gamma_multiply`] instead. pub fn linear_multiply(self, factor: f32) -> Color32 { crate::ecolor_assert!(0.0 <= factor && factor <= 1.0); // As an unfortunate side-effect of using premultiplied alpha From 3ae717e619e58fcd04c1ac22887be8e201b87d1e Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 12 Dec 2022 15:50:31 +0100 Subject: [PATCH 4/9] Remove unused pub use --- crates/eframe/src/web/backend.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/eframe/src/web/backend.rs b/crates/eframe/src/web/backend.rs index e939995d1ac..f139cab7f2f 100644 --- a/crates/eframe/src/web/backend.rs +++ b/crates/eframe/src/web/backend.rs @@ -2,7 +2,6 @@ use egui::{ mutex::{Mutex, MutexGuard}, TexturesDelta, }; -pub use egui::{pos2, Color32}; use crate::{epi, App}; From 27fab9ac7f106060642e88cc571a8c0e8754dc27 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 12 Dec 2022 15:50:43 +0100 Subject: [PATCH 5/9] Remove non-existing crate category --- crates/ecolor/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ecolor/Cargo.toml b/crates/ecolor/Cargo.toml index 4102095c999..ac0aa458b8b 100644 --- a/crates/ecolor/Cargo.toml +++ b/crates/ecolor/Cargo.toml @@ -12,7 +12,7 @@ homepage = "https://github.com/emilk/egui" license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/emilk/egui" -categories = ["mathematics", "encoding", "images"] +categories = ["mathematics", "encoding"] keywords = ["gui", "color", "conversion", "gamedev", "images"] include = ["../LICENSE-APACHE", "../LICENSE-MIT", "**/*.rs", "Cargo.toml"] From 1536482dfc2595bb91ff833c3c55f50d8b282a0d Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 12 Dec 2022 15:54:55 +0100 Subject: [PATCH 6/9] Improve color test with more lines --- crates/egui_demo_lib/src/color_test.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/egui_demo_lib/src/color_test.rs b/crates/egui_demo_lib/src/color_test.rs index a4637f62277..15157ed34af 100644 --- a/crates/egui_demo_lib/src/color_test.rs +++ b/crates/egui_demo_lib/src/color_test.rs @@ -460,21 +460,21 @@ fn paint_fine_lines_and_text(painter: &egui::Painter, mut rect: Rect, color: Col Align2::LEFT_TOP, format!("{:.0}% white", 100.0 * opacity), FontId::proportional(14.0), - Color32::WHITE.linear_multiply(opacity), + Color32::WHITE.gamma_multiply(opacity), ); painter.text( rect.center_top() + vec2(80.0, y), Align2::LEFT_TOP, format!("{:.0}% gray", 100.0 * opacity), FontId::proportional(14.0), - Color32::GRAY.linear_multiply(opacity), + Color32::GRAY.gamma_multiply(opacity), ); painter.text( rect.center_top() + vec2(160.0, y), Align2::LEFT_TOP, format!("{:.0}% black", 100.0 * opacity), FontId::proportional(14.0), - Color32::BLACK.linear_multiply(opacity), + Color32::BLACK.gamma_multiply(opacity), ); y += 20.0; } @@ -495,8 +495,8 @@ fn paint_fine_lines_and_text(painter: &egui::Painter, mut rect: Rect, color: Col rect.max.x = rect.center().x; - rect = rect.shrink(12.0); - for width in [0.5, 1.0, 2.0] { + rect = rect.shrink(16.0); + for width in [0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 4.0] { painter.text( rect.left_top(), Align2::CENTER_CENTER, @@ -517,8 +517,8 @@ fn paint_fine_lines_and_text(painter: &egui::Painter, mut rect: Rect, color: Col Stroke::new(width, color), )); - rect.min.y += 32.0; - rect.max.x -= 32.0; + rect.min.y += 24.0; + rect.max.x -= 24.0; } rect.min.y += 16.0; From 64cf7c12f9956bf4df552f3e21b0223e523e0682 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 12 Dec 2022 15:55:59 +0100 Subject: [PATCH 7/9] Improve the look of thin lines, making them look weaker Before they looked were too strong for the thickness. --- crates/epaint/src/tessellator.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/epaint/src/tessellator.rs b/crates/epaint/src/tessellator.rs index a15a8c79099..37c89e6f810 100644 --- a/crates/epaint/src/tessellator.rs +++ b/crates/epaint/src/tessellator.rs @@ -957,10 +957,9 @@ fn stroke_path( } fn mul_color(color: Color32, factor: f32) -> Color32 { - crate::epaint_assert!(0.0 <= factor && factor <= 1.0); - // As an unfortunate side-effect of using premultiplied alpha - // we need a somewhat expensive conversion to linear space and back. - color.linear_multiply(factor) + // The fast gamma-space multiply also happens to be perceptually better. + // Win-win! + color.gamma_multiply(factor) } // ---------------------------------------------------------------------------- From 9e38dd8fa50928b38552416ba5debc5252da2061 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 12 Dec 2022 15:59:21 +0100 Subject: [PATCH 8/9] Use asserts for shader compilations --- crates/egui_demo_app/src/apps/custom3d_glow.rs | 12 ++++++------ examples/custom_3d_glow/src/main.rs | 8 +++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/crates/egui_demo_app/src/apps/custom3d_glow.rs b/crates/egui_demo_app/src/apps/custom3d_glow.rs index 5d82d75c244..84d61d3d240 100644 --- a/crates/egui_demo_app/src/apps/custom3d_glow.rs +++ b/crates/egui_demo_app/src/apps/custom3d_glow.rs @@ -146,12 +146,12 @@ impl RotatingTriangle { ), ); gl.compile_shader(shader); - if !gl.get_shader_compile_status(shader) { - panic!( - "Failed to compile custom_3d_glow: {}", - gl.get_shader_info_log(shader) - ); - } + assert!( + gl.get_shader_compile_status(shader), + "Failed to compile custom_3d_glow {shader_type}: {}", + gl.get_shader_info_log(shader) + ); + gl.attach_shader(program, shader); shader }) diff --git a/examples/custom_3d_glow/src/main.rs b/examples/custom_3d_glow/src/main.rs index b7a828c526b..79cf16229ba 100644 --- a/examples/custom_3d_glow/src/main.rs +++ b/examples/custom_3d_glow/src/main.rs @@ -145,9 +145,11 @@ impl RotatingTriangle { .expect("Cannot create shader"); gl.shader_source(shader, &format!("{}\n{}", shader_version, shader_source)); gl.compile_shader(shader); - if !gl.get_shader_compile_status(shader) { - panic!("{}", gl.get_shader_info_log(shader)); - } + assert!( + gl.get_shader_compile_status(shader), + "Failed to compile {shader_type}: {}", + gl.get_shader_info_log(shader) + ); gl.attach_shader(program, shader); shader }) From bcfae2ad2033706604a4c2469ae524bd54ed150e Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 12 Dec 2022 16:00:49 +0100 Subject: [PATCH 9/9] Update changelogs --- CHANGELOG.md | 1 + crates/ecolor/CHANGELOG.md | 1 + crates/epaint/CHANGELOG.md | 1 + 3 files changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a199384565d..464221c4aa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG ## 0.20.1 - 2022-12-11 - Fix key-repeat ### Changed 🔧 * `InputState`: all press functions again include key repeates (like in egui 0.19) ([#2429](https://github.com/emilk/egui/pull/2429)). +* Improve the look of thin white lines ([#2437](https://github.com/emilk/egui/pull/2437)). ### Fixed 🐛 * Fix key-repeats for `TextEdit`, `Slider`s, etc ([#2429](https://github.com/emilk/egui/pull/2429)). diff --git a/crates/ecolor/CHANGELOG.md b/crates/ecolor/CHANGELOG.md index 862b16006fc..6dcc6873d86 100644 --- a/crates/ecolor/CHANGELOG.md +++ b/crates/ecolor/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to the `ecolor` crate will be noted in this file. ## Unreleased +* Add `Color32::gamma_multiply` ([#2437](https://github.com/emilk/egui/pull/2437)). ## 0.20.0 - 2022-12-08 diff --git a/crates/epaint/CHANGELOG.md b/crates/epaint/CHANGELOG.md index afe6fb9337c..52ac1405566 100644 --- a/crates/epaint/CHANGELOG.md +++ b/crates/epaint/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to the epaint crate will be documented in this file. ## Unreleased +* Improve the look of thin white lines ([#2437](https://github.com/emilk/egui/pull/2437)). ## 0.20.0 - 2022-12-08