[go: up one dir, main page]

zng-color 0.3.0

Part of the zng project.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! Color filter effect.

use std::fmt;

use zng_layout::{
    context::LayoutMask,
    unit::{about_eq, about_eq_hash, AngleDegree, Factor, FactorUnits, Layout1d, Layout2d, Length, Point},
};
use zng_var::{
    animation::{easing::EasingStep, Transitionable},
    impl_from_and_into_var,
};
use zng_view_api::display_list::{FilterOp, FrameValue};

use crate::{lerp_rgba, Rgba};

/// A color filter or combination of filters.
///
/// # Examples
///
/// ```
/// use zng_color::filter::Filter;
/// use zng_layout::unit::*;
///
/// let filter = Filter::new_opacity(50.pct()).blur(3);
/// ```
///
/// The example above creates a filter that lowers the opacity to `50%` and blurs by `3px`.
#[derive(Clone, Default, PartialEq)]
pub struct Filter {
    filters: Vec<FilterData>,
    needs_layout: bool,
}

impl Filter {
    /// Add an opacity adjustment to the filter, zero is fully transparent, one is the input transparency.
    pub fn opacity<A: Into<Factor>>(self, alpha: A) -> Self {
        let alpha_value = alpha.into().0;
        self.op(FilterOp::Opacity(FrameValue::Value(alpha_value)))
    }

    /// Add a color inversion filter, zero does not invert, one fully inverts.
    pub fn invert<A: Into<Factor>>(self, amount: A) -> Self {
        self.op(FilterOp::Invert(amount.into().0))
    }

    /// Add a blue effect to the filter, the blue `radius` is defined by a [`Length`].
    ///
    /// Relative lengths are calculated by the width of the available space.
    ///
    /// [`Length`]: zng_layout::unit::Length
    pub fn blur<R: Into<Length>>(mut self, radius: R) -> Self {
        self.needs_layout = true;
        self.filters.push(FilterData::Blur(radius.into()));
        self
    }

    /// Add a sepia color effect to the filter, zero is the input color, one is the full desaturated brown look.
    pub fn sepia<A: Into<Factor>>(self, amount: A) -> Self {
        self.op(FilterOp::Sepia(amount.into().0))
    }

    /// Add a grayscale color effect to the filter, zero is the input color, one if the full grayscale.
    pub fn grayscale<A: Into<Factor>>(self, amount: A) -> Self {
        self.op(FilterOp::Grayscale(amount.into().0))
    }

    /// Add a drop-shadow to the effect.
    pub fn drop_shadow<O: Into<Point>, R: Into<Length>, C: Into<Rgba>>(mut self, offset: O, blur_radius: R, color: C) -> Self {
        self.needs_layout = true;
        self.filters.push(FilterData::DropShadow {
            offset: offset.into(),
            blur_radius: blur_radius.into(),
            color: color.into(),
        });
        self
    }

    /// Add a brightness adjustment to the filter, zero removes all brightness, one is the input brightness.
    pub fn brightness<A: Into<Factor>>(self, amount: A) -> Self {
        self.op(FilterOp::Brightness(amount.into().0))
    }

    /// Add a contrast adjustment to the filter, zero removes all contrast, one is the input contrast.
    pub fn contrast<A: Into<Factor>>(self, amount: A) -> Self {
        self.op(FilterOp::Contrast(amount.into().0))
    }

    /// Add a saturation adjustment to the filter, zero fully desaturates, one is the input saturation.
    pub fn saturate<A: Into<Factor>>(self, amount: A) -> Self {
        self.op(FilterOp::Saturate(amount.into().0))
    }

    /// Add a filter that adds the `angle` to each color [`hue`] value.
    ///
    /// [`hue`]: crate::Hsla::hue
    pub fn hue_rotate<A: Into<AngleDegree>>(self, angle: A) -> Self {
        self.op(FilterOp::HueRotate(angle.into().0))
    }

    /// Add a filter that fills the pixel space with `color`.
    pub fn flood<C: Into<Rgba>>(self, color: C) -> Self {
        self.op(FilterOp::Flood(color.into()))
    }

    /// Custom filter.
    ///
    /// The color matrix is in the format of SVG color matrix, [0..5] is the first matrix row.
    pub fn color_matrix<M: Into<ColorMatrix>>(self, matrix: M) -> Self {
        self.op(FilterOp::ColorMatrix(matrix.into().0))
    }
}

impl Filter {
    /// New default empty.
    pub const fn new() -> Filter {
        Self {
            filters: vec![],
            needs_layout: false,
        }
    }

    /// New [`Filter::opacity`].
    pub fn new_opacity<A: Into<Factor>>(alpha: A) -> Filter {
        Filter::default().opacity(alpha)
    }
    /// New [`Filter::invert`].
    pub fn new_invert<A: Into<Factor>>(amount: A) -> Filter {
        Filter::default().invert(amount)
    }
    /// New [`Filter::blur`].
    pub fn new_blur<R: Into<Length>>(radius: R) -> Filter {
        Filter::default().blur(radius)
    }
    /// New [`Filter::sepia`].
    pub fn new_sepia<A: Into<Factor>>(amount: A) -> Filter {
        Filter::default().sepia(amount)
    }
    /// New [`Filter::grayscale`].
    pub fn new_grayscale<A: Into<Factor>>(amount: A) -> Filter {
        Filter::default().grayscale(amount)
    }
    /// New [`Filter::drop_shadow`].
    pub fn new_drop_shadow<O: Into<Point>, R: Into<Length>, C: Into<Rgba>>(offset: O, blur_radius: R, color: C) -> Filter {
        Filter::default().drop_shadow(offset, blur_radius, color)
    }
    /// New [`Filter::brightness`].
    pub fn new_brightness<A: Into<Factor>>(amount: A) -> Filter {
        Filter::default().brightness(amount)
    }
    /// New [`Filter::contrast`].
    pub fn new_contrast<A: Into<Factor>>(amount: A) -> Filter {
        Filter::default().contrast(amount)
    }
    /// New [`Filter::saturate`].
    pub fn new_saturate<A: Into<Factor>>(amount: A) -> Filter {
        Filter::default().saturate(amount)
    }
    /// New [`Filter::hue_rotate`].
    pub fn new_hue_rotate<A: Into<AngleDegree>>(angle: A) -> Filter {
        Filter::default().hue_rotate(angle)
    }
    /// New [`Filter::flood`].
    pub fn new_flood<C: Into<Rgba>>(color: C) -> Filter {
        Filter::default().flood(color)
    }
    /// New [`Filter::color_matrix`].
    pub fn new_color_matrix<M: Into<ColorMatrix>>(matrix: M) -> Filter {
        Filter::default().color_matrix(matrix)
    }
}

impl Filter {
    fn op(mut self, op: FilterOp) -> Self {
        self.filters.push(FilterData::Op(op));
        self
    }

    /// Compute a [`RenderFilter`] in the current [`LAYOUT`] context.
    ///
    /// Most filters convert one-to-one, effects that have a [`Length`] value use the
    /// layout context to calculate relative values.
    ///
    /// Relative blur radius lengths are calculated using the `constraints().fill_size().width` value.
    ///
    /// [`LAYOUT`]: zng_layout::context::LAYOUT
    /// [`Length`]: zng_layout::unit::Length
    pub fn layout(&self) -> RenderFilter {
        self.filters
            .iter()
            .map(|f| match f {
                FilterData::Op(op) => *op,
                FilterData::Blur(l) => {
                    let l = l.layout_f32_x();
                    FilterOp::Blur(l, l)
                }
                FilterData::DropShadow {
                    offset,
                    blur_radius,
                    color,
                } => FilterOp::DropShadow {
                    offset: offset.layout().to_vector().cast(),
                    color: *color,
                    blur_radius: blur_radius.layout_f32_x(),
                },
            })
            .collect()
    }

    /// Compute a [`RenderFilter`] if the filter is not affected by layout.
    pub fn try_render(&self) -> Option<RenderFilter> {
        if self.needs_layout {
            return None;
        }

        let mut r = Vec::with_capacity(self.filters.len());

        for f in &self.filters {
            match f {
                FilterData::Op(op) => r.push(*op),
                FilterData::Blur(_) | FilterData::DropShadow { .. } => unreachable!(),
            }
        }

        Some(r)
    }

    /// Returns `true` if this filter is affected by the layout context where it is evaluated.
    pub fn needs_layout(&self) -> bool {
        self.needs_layout
    }
}

impl fmt::Debug for Filter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            f.debug_tuple("Filter").field(&self.filters).finish()
        } else if self.filters.is_empty() {
            write!(f, "[]")
        } else {
            write!(f, "{:?}", self.filters[0])?;
            for filter in &self.filters[1..] {
                write!(f, ".{filter:?}")?;
            }
            Ok(())
        }
    }
}
impl Layout2d for Filter {
    type Px = RenderFilter;

    fn layout_dft(&self, _: Self::Px) -> Self::Px {
        self.layout()
    }

    fn affect_mask(&self) -> LayoutMask {
        let mut mask = LayoutMask::empty();
        for f in &self.filters {
            match f {
                FilterData::Op(_) => {}
                FilterData::Blur(l) => mask |= l.affect_mask(),
                FilterData::DropShadow { offset, blur_radius, .. } => {
                    mask |= offset.affect_mask();
                    mask |= blur_radius.affect_mask();
                }
            }
        }
        mask
    }
}

/// A computed [`Filter`], ready for Webrender.
pub type RenderFilter = Vec<FilterOp>;

#[derive(Clone, PartialEq)]
enum FilterData {
    Op(FilterOp),
    Blur(Length),
    DropShadow { offset: Point, blur_radius: Length, color: Rgba },
}
impl fmt::Debug for FilterData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            write!(f, "FilterData::")?;
            match self {
                FilterData::Op(op) => f.debug_tuple("Op").field(op).finish(),
                FilterData::Blur(l) => f.debug_tuple("Blur").field(l).finish(),
                FilterData::DropShadow {
                    offset,
                    blur_radius,
                    color,
                } => f
                    .debug_struct("DropShadow")
                    .field("offset", offset)
                    .field("blur_radius", blur_radius)
                    .field("color", color)
                    .finish(),
            }
        } else {
            fn bool_or_pct(func: &'static str, value: f32, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                if value.abs() < 0.0001 {
                    write!(f, "{func}(false)")
                } else if (value - 1.0).abs() < 0.0001 {
                    write!(f, "{func}(true)")
                } else {
                    write!(f, "{func}({}.pct())", value * 100.0)
                }
            }
            match self {
                FilterData::Op(op) => match op {
                    FilterOp::Blur(w, _) => write!(f, "blur({w})"),
                    FilterOp::Brightness(b) => write!(f, "brightness({}.pct())", b * 100.0),
                    FilterOp::Contrast(c) => write!(f, "brightness({}.pct())", c * 100.0),
                    FilterOp::Grayscale(c) => bool_or_pct("grayscale", *c, f),
                    FilterOp::HueRotate(d) => write!(f, "hue_rotate({d}.deg())"),
                    FilterOp::Invert(i) => bool_or_pct("invert", *i, f),
                    FilterOp::Opacity(o) => write!(f, "opacity({}.pct())", *o.value() * 100.0),
                    FilterOp::Saturate(s) => write!(f, "saturate({}.pct())", s * 100.0),
                    FilterOp::Sepia(s) => bool_or_pct("sepia", *s, f),
                    FilterOp::DropShadow {
                        offset,
                        color,
                        blur_radius,
                    } => write!(f, "drop_shadow(({}, {}), {}, {})", offset.x, offset.y, blur_radius, *color),
                    FilterOp::ColorMatrix(m) => write!(f, "color_matrix({:?})", ColorMatrix(*m)),
                    FilterOp::Flood(c) => write!(f, "flood({})", *c),
                },
                FilterData::Blur(l) => write!(f, "blur({l:?})"),
                FilterData::DropShadow {
                    offset,
                    blur_radius,
                    color,
                } => {
                    write!(f, "drop_shadow({offset:?}, {blur_radius:?}, {color:?})")
                }
            }
        }
    }
}

fn lerp_frame_value<T: Transitionable>(s: FrameValue<T>, to: &FrameValue<T>, step: EasingStep) -> FrameValue<T> {
    let mut bind_data = None;
    let mut value = match s {
        FrameValue::Bind { id, value, animating } => {
            bind_data = Some((id, animating));
            value
        }
        FrameValue::Value(v) => v,
    };

    value = value.lerp(to.value(), step);

    if step < 1.fct() {
        if let Some((id, animating)) = bind_data {
            FrameValue::Bind { id, value, animating }
        } else {
            FrameValue::Value(value)
        }
    } else {
        match to {
            FrameValue::Bind { id, animating, .. } => FrameValue::Bind {
                id: *id,
                value,
                animating: *animating,
            },
            FrameValue::Value(_) => FrameValue::Value(value),
        }
    }
}

fn lerp_filter_op(mut s: FilterOp, to: &FilterOp, step: EasingStep) -> FilterOp {
    match (&mut s, to) {
        (FilterOp::Blur(x, y), FilterOp::Blur(xb, yb)) => {
            *x = x.lerp(xb, step);
            *y = y.lerp(yb, step);
        }
        (FilterOp::Brightness(a), FilterOp::Brightness(b)) => {
            *a = a.lerp(b, step);
        }
        (FilterOp::Contrast(a), FilterOp::Contrast(b)) => {
            *a = a.lerp(b, step);
        }
        (FilterOp::Grayscale(a), FilterOp::Grayscale(b)) => {
            *a = a.lerp(b, step);
        }
        (FilterOp::HueRotate(a), FilterOp::HueRotate(b)) => {
            *a = a.lerp(b, step);
        }
        (FilterOp::Invert(a), FilterOp::Invert(b)) => {
            *a = a.lerp(b, step);
        }
        (FilterOp::Opacity(a), FilterOp::Opacity(b)) => {
            *a = lerp_frame_value(*a, b, step);
        }
        (FilterOp::Saturate(a), FilterOp::Saturate(b)) => {
            *a = a.lerp(b, step);
        }
        (FilterOp::Sepia(a), FilterOp::Sepia(b)) => {
            *a = a.lerp(b, step);
        }
        (
            FilterOp::DropShadow {
                offset,
                color,
                blur_radius,
            },
            FilterOp::DropShadow {
                offset: to_offset,
                color: to_color,
                blur_radius: to_blur,
            },
        ) => {
            *offset = Transitionable::lerp(*offset, to_offset, step);
            *color = lerp_rgba(*color, *to_color, step);
            *blur_radius = blur_radius.lerp(to_blur, step);
        }
        (FilterOp::ColorMatrix(a), FilterOp::ColorMatrix(b)) => {
            for (a, b) in a.iter_mut().zip(b) {
                *a = a.lerp(b, step);
            }
        }
        (FilterOp::Flood(a), FilterOp::Flood(b)) => {
            *a = lerp_rgba(*a, *b, step);
        }
        (a, b) => {
            if step >= 1.fct() {
                *a = *b
            }
        }
    }
    s
}

impl Transitionable for Filter {
    fn lerp(mut self, to: &Self, step: EasingStep) -> Self {
        let end = step >= 1.fct();

        for z in self.filters.iter_mut().zip(&to.filters) {
            match z {
                (FilterData::Op(a), FilterData::Op(b)) => *a = lerp_filter_op(*a, b, step),
                (FilterData::Blur(a), FilterData::Blur(b)) => {
                    *a = a.clone().lerp(b, step);
                }
                (
                    FilterData::DropShadow {
                        offset,
                        blur_radius,
                        color,
                    },
                    FilterData::DropShadow {
                        offset: offset_b,
                        blur_radius: blur_b,
                        color: color_b,
                    },
                ) => {
                    *offset = offset.clone().lerp(offset_b, step);
                    *blur_radius = blur_radius.clone().lerp(blur_b, step);
                    *color = color.lerp(color_b, step);
                }
                (a, b) => {
                    if end {
                        *a = b.clone();
                    }
                }
            }
        }

        if end {
            match self.filters.len().cmp(&to.filters.len()) {
                std::cmp::Ordering::Less => self.filters.truncate(to.filters.len()),
                std::cmp::Ordering::Greater => self.filters.extend(to.filters[self.filters.len()..].iter().cloned()),
                std::cmp::Ordering::Equal => {}
            }

            self.needs_layout = to.needs_layout;
        }

        self
    }
}

/// Represents a custom color filter.
///
/// The color matrix is in the format of SVG color matrix, [0..5] is the first matrix row.
#[derive(Clone, Copy)]
pub struct ColorMatrix(pub [f32; 20]);
impl ColorMatrix {
    /// Matrix that does not alter any color.
    #[rustfmt::skip]
    pub const fn identity() -> Self {
        ColorMatrix([
            1.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 1.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 1.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 1.0, 0.0,
        ])
    }
}
impl fmt::Debug for ColorMatrix {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self == &ColorMatrix::identity() {
            if f.alternate() {
                write!(f, "ColorMatrix::")?;
            }
            return write!(f, "identity()");
        }

        if f.alternate() {
            write!(f, "ColorMatrix(")?;
        }
        writeln!(f, "[")?;
        for row in 0..4 {
            write!(f, "    ")?;
            for column in 0..5 {
                let v = self.0[row * column];
                if v < 0.0 {
                    write!(f, "{v:.3}, ")?;
                } else {
                    write!(f, " {v:.3}, ")?;
                }
            }
            writeln!(f)?;
        }
        write!(f, "]")?;
        if f.alternate() {
            write!(f, ")")?;
        }
        Ok(())
    }
}
impl_from_and_into_var! {
    fn from(matrix: [f32; 20]) -> ColorMatrix {
        ColorMatrix(matrix)
    }
}
impl Default for ColorMatrix {
    fn default() -> Self {
        Self::identity()
    }
}
impl PartialEq for ColorMatrix {
    fn eq(&self, other: &Self) -> bool {
        self.0.iter().zip(&other.0).all(|(&a, &b)| about_eq(a, b, 0.00001))
    }
}
impl std::hash::Hash for ColorMatrix {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        for f in self.0 {
            about_eq_hash(f, 0.00001, state);
        }
    }
}
impl Transitionable for ColorMatrix {
    fn lerp(mut self, to: &Self, step: EasingStep) -> Self {
        for (a, b) in self.0.iter_mut().zip(&to.0) {
            *a = a.lerp(b, step);
        }
        self
    }
}