Skip to content

Commit

Permalink
Improve the look of thin lines, making them look weaker (#2437)
Browse files Browse the repository at this point in the history
* Revert "fix all clippy lints and remove them from allow list in cranky (#2419)"

This reverts commit 930ef2d.

* Explain the cranky lints better

* Add Color32::gamma_multiply

* Remove unused pub use

* Remove non-existing crate category

* Improve color test with more lines

* Improve the look of thin lines, making them look weaker

Before they looked were too strong for the thickness.

* Use asserts for shader compilations

* Update changelogs
  • Loading branch information
emilk authored Dec 12, 2022
1 parent 6c4fc50 commit e0b5bb1
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
8 changes: 7 additions & 1 deletion Cranky.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ 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",
"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",
]
1 change: 1 addition & 0 deletions crates/ecolor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/ecolor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
24 changes: 22 additions & 2 deletions crates/ecolor/src/color32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,29 @@ 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..=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()
Expand Down
2 changes: 1 addition & 1 deletion crates/ecolor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/ecolor/src/rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

Expand Down
1 change: 0 additions & 1 deletion crates/eframe/src/web/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use egui::{
mutex::{Mutex, MutexGuard},
TexturesDelta,
};
pub use egui::{pos2, Color32};

use crate::{epi, App};

Expand Down
3 changes: 2 additions & 1 deletion crates/egui_demo_app/src/apps/custom3d_glow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ impl RotatingTriangle {
gl.compile_shader(shader);
assert!(
gl.get_shader_compile_status(shader),
"Failed to compile custom_3d_glow: {}",
"Failed to compile custom_3d_glow {shader_type}: {}",
gl.get_shader_info_log(shader)
);

gl.attach_shader(program, shader);
shader
})
Expand Down
14 changes: 7 additions & 7 deletions crates/egui_demo_lib/src/color_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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,
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions crates/epaint/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions crates/epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

// ----------------------------------------------------------------------------
Expand Down
8 changes: 5 additions & 3 deletions examples/custom_3d_glow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down

0 comments on commit e0b5bb1

Please sign in to comment.