From 5599bd44e09a173406f59e7b37a8068b028d93cb Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 2 Jun 2023 09:57:50 +0200 Subject: [PATCH] Remove Color::opaque It is not a right name and i don't think it is a so common operation --- CHANGELOG.md | 2 +- api/cpp/cbindgen.rs | 3 -- api/cpp/include/slint_brush.h | 39 +------------------ api/cpp/include/slint_color.h | 23 +---------- docs/language/src/reference/types.md | 5 --- internal/compiler/expression_tree.rs | 7 ---- internal/compiler/generator/cpp.rs | 3 -- internal/compiler/generator/rust.rs | 5 --- .../llr/optim_passes/inline_expressions.rs | 1 - internal/compiler/lookup.rs | 1 - internal/core/graphics/brush.rs | 28 ------------- internal/core/graphics/color.rs | 39 ------------------- internal/interpreter/eval.rs | 14 ------- tests/cases/types/brush.slint | 2 +- 14 files changed, 5 insertions(+), 167 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed46b56cb..0d9fd58cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ All notable changes to this project are documented in this file. - Added `Switch` widget. - Added boolean `font-italic` property to `Text` and `TextInput`. - Added `select-all()`, `cut()`, `copy()`, and `paste() to `TextInput`, `LineEdit`, and `TextEdit`. - - Added functions on color: `transparentize`, `mix`, `opaque`, `with-alpha` + - Added functions on color: `transparentize`, `mix`, `with-alpha` ### Rust diff --git a/api/cpp/cbindgen.rs b/api/cpp/cbindgen.rs index 1b243633a..64cd424ba 100644 --- a/api/cpp/cbindgen.rs +++ b/api/cpp/cbindgen.rs @@ -204,7 +204,6 @@ fn gen_corelib( "slint_color_brighter", "slint_color_darker", "slint_color_transparentize", - "slint_color_opaque", "slint_color_mix", "slint_color_with_alpha", "slint_image_size", @@ -295,7 +294,6 @@ fn gen_corelib( ( vec!["Color", "slint_color_brighter", "slint_color_darker", "slint_color_transparentize", - "slint_color_opaque", "slint_color_mix", "slint_color_with_alpha",], vec![], @@ -350,7 +348,6 @@ fn gen_corelib( "slint_color_brighter", "slint_color_darker", "slint_color_transparentize", - "slint_color_opaque", "slint_color_mix", "slint_color_with_alpha", "slint_image_size", diff --git a/api/cpp/include/slint_brush.h b/api/cpp/include/slint_brush.h index dc6feb824..edaa69360 100644 --- a/api/cpp/include/slint_brush.h +++ b/api/cpp/include/slint_brush.h @@ -141,20 +141,7 @@ public: /// /// The reference is the opacity's normalized value as `u8` and \a factor is /// clamped to be between `0.0` and `1.0` before applying it. - /// - /// For _increasing_ the opacity, see Brush::opaque(float) and - /// Brush::with_alpha(float). inline Brush transparentize(float factor) const; - /// Returns a new version of this brush with the opacity increased by \a factor, - /// meaning the new opacity will be scaled up by `1.0 + factor`. - /// - /// The reference is the opacity's normalized value as `u8` and \a factor is - /// changed to be at least `0.0` before applying it, and thus the current - /// value cannot be decreased. - /// - /// For _decreasing_ the opacity, see Brush::transparentize(float) - /// and Brush::with_alpha(float). - inline Brush opaque(float factor) const; /// Returns a new version of this brush with the related color's opacities /// set to \a alpha. @@ -249,7 +236,7 @@ inline Brush Brush::transparentize(float factor) const switch (data.tag) { case Tag::SolidColor: cbindgen_private::types::slint_color_transparentize(&data.solid_color._0, factor, - &result.data.solid_color._0); + &result.data.solid_color._0); break; case Tag::LinearGradient: for (std::size_t i = 1; i < data.linear_gradient._0.size(); ++i) { @@ -269,30 +256,6 @@ inline Brush Brush::transparentize(float factor) const return result; } -inline Brush Brush::opaque(float factor) const -{ - Brush result = *this; - switch (data.tag) { - case Tag::SolidColor: - cbindgen_private::types::slint_color_opaque(&data.solid_color._0, factor, - &result.data.solid_color._0); - break; - case Tag::LinearGradient: - for (std::size_t i = 1; i < data.linear_gradient._0.size(); ++i) { - cbindgen_private::types::slint_color_opaque(&data.linear_gradient._0[i].color, factor, - &result.data.linear_gradient._0[i].color); - } - break; - case Tag::RadialGradient: - for (std::size_t i = 0; i < data.linear_gradient._0.size(); ++i) { - cbindgen_private::types::slint_color_opaque(&data.radial_gradient._0[i].color, factor, - &result.data.radial_gradient._0[i].color); - } - break; - } - return result; -} - inline Brush Brush::with_alpha(float alpha) const { Brush result = *this; diff --git a/api/cpp/include/slint_color.h b/api/cpp/include/slint_color.h index bb6625518..aa98c4ab8 100644 --- a/api/cpp/include/slint_color.h +++ b/api/cpp/include/slint_color.h @@ -152,20 +152,7 @@ public: /// /// The reference is the opacity's normalized value as `u8` and \a factor is /// clamped to be between `0.0` and `1.0` before applying it. - /// - /// For _increasing_ the opacity, see Color::opaque(float) and - /// Color::with_alpha(float). inline Color transparentize(float factor) const; - /// Returns a new version of this color with the opacity increased by \a factor, - /// meaning the new opacity will be scaled up by `1.0 + factor`. - /// - /// The reference is the opacity's normalized value as `u8` and \a factor is - /// changed to be at least `0.0` before applying it, and thus the current - /// value cannot be decreased. - /// - /// For _decreasing_ the opacity, see Color::transparentize(float) and - /// Color::with_alpha(float). - inline Color opaque(float factor) const; /// Returns a new color that is a mix of \a this and \a other, with a proportion /// factor given by \a factor (which will be clamped to be between `0.0` and `1.0`). @@ -190,7 +177,8 @@ public: #if !defined(DOXYGEN) // FIXME: we need this to create GradientStop - operator const cbindgen_private::types::Color &() const { + operator const cbindgen_private::types::Color &() const + { return inner; } #endif @@ -222,13 +210,6 @@ inline Color Color::transparentize(float factor) const return result; } -inline Color Color::opaque(float factor) const -{ - Color result; - cbindgen_private::types::slint_color_opaque(&inner, factor, &result.inner); - return result; -} - inline Color Color::mix(const Color &other, float factor) const { Color result; diff --git a/docs/language/src/reference/types.md b/docs/language/src/reference/types.md index eb660ad1c..010120d8e 100644 --- a/docs/language/src/reference/types.md +++ b/docs/language/src/reference/types.md @@ -86,11 +86,6 @@ All colors and brushes define the following methods: Returns a new color that is a mix of this color and `other`, with a proportion factor given by \a factor (which will be clamped to be between `0.0` and `1.0`). -- **`opaque(factor: float) -> brush`** - - Returns a new color derived from this color with the opacity increased by `factor`, - meaning the new opacity will be scaled up by `1.0 + factor`. - - **`transparentize(factor: float) -> brush`** Returns a new color with the opacity decreased by `factor`, diff --git a/internal/compiler/expression_tree.rs b/internal/compiler/expression_tree.rs index 93dc42180..47b30aab4 100644 --- a/internal/compiler/expression_tree.rs +++ b/internal/compiler/expression_tree.rs @@ -48,7 +48,6 @@ pub enum BuiltinFunction { ColorBrighter, ColorDarker, ColorTransparentize, - ColorOpaque, ColorMix, ColorWithAlpha, ImageSize, @@ -154,10 +153,6 @@ impl BuiltinFunction { return_type: Box::new(Type::Brush), args: vec![Type::Brush, Type::Float32], }, - BuiltinFunction::ColorOpaque => Type::Function { - return_type: Box::new(Type::Brush), - args: vec![Type::Brush, Type::Float32], - }, BuiltinFunction::ColorMix => Type::Function { return_type: Box::new(Type::Color), args: vec![Type::Color, Type::Color, Type::Float32], @@ -236,7 +231,6 @@ impl BuiltinFunction { BuiltinFunction::ColorBrighter | BuiltinFunction::ColorDarker | BuiltinFunction::ColorTransparentize - | BuiltinFunction::ColorOpaque | BuiltinFunction::ColorMix | BuiltinFunction::ColorWithAlpha => true, // ImageSize is pure, except when loading images via the network. Then the initial size will be 0/0 and @@ -288,7 +282,6 @@ impl BuiltinFunction { BuiltinFunction::ColorBrighter | BuiltinFunction::ColorDarker | BuiltinFunction::ColorTransparentize - | BuiltinFunction::ColorOpaque | BuiltinFunction::ColorMix | BuiltinFunction::ColorWithAlpha => true, BuiltinFunction::ImageSize => true, diff --git a/internal/compiler/generator/cpp.rs b/internal/compiler/generator/cpp.rs index b7b9163d1..a99aa287d 100644 --- a/internal/compiler/generator/cpp.rs +++ b/internal/compiler/generator/cpp.rs @@ -2713,9 +2713,6 @@ fn compile_builtin_function_call( BuiltinFunction::ColorTransparentize => { format!("{}.transparentize({})", a.next().unwrap(), a.next().unwrap()) } - BuiltinFunction::ColorOpaque => { - format!("{}.opaque({})", a.next().unwrap(), a.next().unwrap()) - } BuiltinFunction::ColorMix => { format!("{}.mix({}, {})", a.next().unwrap(), a.next().unwrap(), a.next().unwrap()) } diff --git a/internal/compiler/generator/rust.rs b/internal/compiler/generator/rust.rs index a0ef6e4f2..c1b34a8bc 100644 --- a/internal/compiler/generator/rust.rs +++ b/internal/compiler/generator/rust.rs @@ -2370,11 +2370,6 @@ fn compile_builtin_function_call( let factor = a.next().unwrap(); quote!(#x.transparentize(#factor as f32)) } - BuiltinFunction::ColorOpaque => { - let x = a.next().unwrap(); - let factor = a.next().unwrap(); - quote!(#x.opaque(#factor as f32)) - } BuiltinFunction::ColorMix => { let x = a.next().unwrap(); let y = a.next().unwrap(); diff --git a/internal/compiler/llr/optim_passes/inline_expressions.rs b/internal/compiler/llr/optim_passes/inline_expressions.rs index 1cfb15038..dce0bd9ee 100644 --- a/internal/compiler/llr/optim_passes/inline_expressions.rs +++ b/internal/compiler/llr/optim_passes/inline_expressions.rs @@ -94,7 +94,6 @@ fn builtin_function_cost(function: &BuiltinFunction) -> isize { BuiltinFunction::ColorBrighter => 50, BuiltinFunction::ColorDarker => 50, BuiltinFunction::ColorTransparentize => 50, - BuiltinFunction::ColorOpaque => 50, BuiltinFunction::ColorMix => 50, BuiltinFunction::ColorWithAlpha => 50, BuiltinFunction::ImageSize => 50, diff --git a/internal/compiler/lookup.rs b/internal/compiler/lookup.rs index 6e9523aaa..312ce5407 100644 --- a/internal/compiler/lookup.rs +++ b/internal/compiler/lookup.rs @@ -858,7 +858,6 @@ impl<'a> LookupObject for ColorExpression<'a> { None.or_else(|| f("brighter", member_function(BuiltinFunction::ColorBrighter))) .or_else(|| f("darker", member_function(BuiltinFunction::ColorDarker))) .or_else(|| f("transparentize", member_function(BuiltinFunction::ColorTransparentize))) - .or_else(|| f("opaque", member_function(BuiltinFunction::ColorOpaque))) .or_else(|| f("with-alpha", member_function(BuiltinFunction::ColorWithAlpha))) .or_else(|| f("mix", member_function(BuiltinFunction::ColorMix))) } diff --git a/internal/core/graphics/brush.rs b/internal/core/graphics/brush.rs index 8ffaea92c..06b51edbc 100644 --- a/internal/core/graphics/brush.rs +++ b/internal/core/graphics/brush.rs @@ -131,9 +131,6 @@ impl Brush { /// /// The reference is the opacity's normalized value as `u8` and `factor` is /// clamped to be between `0.0` and `1.0` before applying it. - /// - /// For _increasing_ the opacity, see [`opaque`](fn@Brush::opaque) and - /// [`with_alpha`](fn@Brush::with_alpha). #[must_use] pub fn transparentize(&self, amount: f32) -> Self { match self { @@ -153,31 +150,6 @@ impl Brush { } } - /// Returns a new version of this brush with the opacity increased by `factor`, - /// meaning the new opacity will be scaled up by `1.0 + factor`. - /// - /// The reference is the opacity's normalized value as `u8` and `factor` is - /// changed to be at least `0.0` before applying it, and thus the current - /// value cannot be decreased. - /// - /// For _decreasing_ the opacity, see [`transparentize`](fn@Brush::transparentize) and - /// [`with_alpha`](fn@Brush::with_alpha). - #[must_use] - pub fn opaque(&self, amount: f32) -> Self { - match self { - Brush::SolidColor(c) => Brush::SolidColor(c.opaque(amount)), - Brush::LinearGradient(g) => Brush::LinearGradient(LinearGradientBrush::new( - g.angle(), - g.stops() - .map(|s| GradientStop { color: s.color.opaque(amount), position: s.position }), - )), - Brush::RadialGradient(g) => Brush::RadialGradient(RadialGradientBrush::new_circle( - g.stops() - .map(|s| GradientStop { color: s.color.opaque(amount), position: s.position }), - )), - } - } - /// Returns a new version of this brush with the related color's opacities /// set to `alpha`. #[must_use] diff --git a/internal/core/graphics/color.rs b/internal/core/graphics/color.rs index 323bf00dd..a6c756f91 100644 --- a/internal/core/graphics/color.rs +++ b/internal/core/graphics/color.rs @@ -199,9 +199,6 @@ impl Color { /// The reference is the opacity's normalized value as `u8` and `factor` is /// clamped to be between `0.0` and `1.0` before applying it. /// - /// For _increasing_ the opacity, see [`opaque`](fn@Color::opaque) and - /// [`with_alpha`](fn@Color::with_alpha). - /// /// # Examples /// Decreasing the opacity of a red color by half: /// ``` @@ -223,37 +220,6 @@ impl Color { rgba.into() } - /// Returns a new version of this color with the opacity increased by `factor`, - /// meaning the new opacity will be scaled up by `1.0 + factor`. - /// - /// The reference is the opacity's normalized value as `u8` and `factor` is - /// changed to be at least `0.0` before applying it, and thus the current - /// value cannot be decreased. - /// - /// For _decreasing_ the opacity, see [`transparentize`](fn@Color::transparentize) and - /// [`with_alpha`](fn@Color::with_alpha). - /// - /// # Examples - /// Increasing the opacity of a red color by 100% (doubling it): - /// ``` - /// # use i_slint_core::graphics::Color; - /// let red = Color::from_argb_u8(128, 255, 0, 0); - /// assert_eq!(red.opaque(1.0), Color::from_argb_u8(255, 255, 0, 0)); - /// ``` - /// - /// Increasing the opacity of a blue color by 20% of the current value: - /// ``` - /// # use i_slint_core::graphics::Color; - /// let blue = Color::from_argb_u8(150, 0, 0, 255); - /// assert_eq!(blue.opaque(0.2), Color::from_argb_u8(180, 0, 0, 255)); - /// ``` - #[must_use] - pub fn opaque(&self, factor: f32) -> Self { - let mut rgba: RgbaColor = (*self).into(); - rgba.alpha = scale_u8(rgba.alpha, 1.0 + f32::max(factor, 0.0)); - rgba.into() - } - /// Returns a new color that is a mix of `self` and `other`, with a proportion /// factor given by `factor` (which will be clamped to be between `0.0` and `1.0`). /// @@ -456,11 +422,6 @@ pub(crate) mod ffi { core::ptr::write(out, col.transparentize(factor)) } - #[no_mangle] - pub unsafe extern "C" fn slint_color_opaque(col: &Color, factor: f32, out: *mut Color) { - core::ptr::write(out, col.opaque(factor)) - } - #[no_mangle] pub unsafe extern "C" fn slint_color_mix( col1: &Color, diff --git a/internal/interpreter/eval.rs b/internal/interpreter/eval.rs index 9246a4fa1..88f51bf28 100644 --- a/internal/interpreter/eval.rs +++ b/internal/interpreter/eval.rs @@ -694,20 +694,6 @@ fn call_builtin_function( panic!("First argument not a color"); } } - BuiltinFunction::ColorOpaque => { - if arguments.len() != 2 { - panic!("internal error: incorrect argument count to ColorFaded") - } - if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) { - if let Value::Number(factor) = eval_expression(&arguments[1], local_context) { - brush.opaque(factor as _).into() - } else { - panic!("Second argument not a number"); - } - } else { - panic!("First argument not a color"); - } - } BuiltinFunction::ColorMix => { if arguments.len() != 3 { panic!("internal error: incorrect argument count to ColorMix") diff --git a/tests/cases/types/brush.slint b/tests/cases/types/brush.slint index 3c5c47170..a6626bf9f 100644 --- a/tests/cases/types/brush.slint +++ b/tests/cases/types/brush.slint @@ -7,7 +7,7 @@ Test := Rectangle { // allow to use brighter and darker on a brush property ligher: true ? color_brush.brighter(50%) : color_brush.darker(50%); // allow to use `transparentize` and `opaque` on brushes - property seethru: true ? color_brush.transparentize(30%) : color_brush.opaque(200%); + property seethru: true ? color_brush.transparentize(30%) : color_brush.transparentize(150%); // allow to use `with_alpha` on brushes property invisible: color_brush.with-alpha(0%);