Remove Color::opaque

It is not a right name and i don't think it is a so common operation
This commit is contained in:
Olivier Goffart 2023-06-02 09:57:50 +02:00 committed by Olivier Goffart
parent cb4ef70d33
commit 5599bd44e0
14 changed files with 5 additions and 167 deletions

View file

@ -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

View file

@ -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",

View file

@ -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.
@ -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;

View file

@ -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;

View file

@ -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`,

View file

@ -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,

View file

@ -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())
}

View file

@ -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();

View file

@ -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,

View file

@ -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)))
}

View file

@ -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]

View file

@ -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<u8> = (*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,

View file

@ -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")

View file

@ -7,7 +7,7 @@ Test := Rectangle {
// allow to use brighter and darker on a brush
property<brush> ligher: true ? color_brush.brighter(50%) : color_brush.darker(50%);
// allow to use `transparentize` and `opaque` on brushes
property<brush> seethru: true ? color_brush.transparentize(30%) : color_brush.opaque(200%);
property<brush> seethru: true ? color_brush.transparentize(30%) : color_brush.transparentize(150%);
// allow to use `with_alpha` on brushes
property<brush> invisible: color_brush.with-alpha(0%);