1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | #include "flutter/lib/ui/painting/color_filter.h" |
6 | |
7 | #include "flutter/lib/ui/ui_dart_state.h" |
8 | #include "third_party/tonic/converter/dart_converter.h" |
9 | #include "third_party/tonic/dart_args.h" |
10 | #include "third_party/tonic/dart_binding_macros.h" |
11 | #include "third_party/tonic/dart_library_natives.h" |
12 | |
13 | namespace flutter { |
14 | |
15 | static void ColorFilter_constructor(Dart_NativeArguments args) { |
16 | UIDartState::ThrowIfUIOperationsProhibited(); |
17 | DartCallConstructor(&ColorFilter::Create, args); |
18 | } |
19 | |
20 | IMPLEMENT_WRAPPERTYPEINFO(ui, ColorFilter); |
21 | |
22 | #define FOR_EACH_BINDING(V) \ |
23 | V(ColorFilter, initMode) \ |
24 | V(ColorFilter, initMatrix) \ |
25 | V(ColorFilter, initSrgbToLinearGamma) \ |
26 | V(ColorFilter, initLinearToSrgbGamma) |
27 | |
28 | FOR_EACH_BINDING(DART_NATIVE_CALLBACK) |
29 | |
30 | void ColorFilter::RegisterNatives(tonic::DartLibraryNatives* natives) { |
31 | natives->Register( |
32 | {{"ColorFilter_constructor" , ColorFilter_constructor, 1, true}, |
33 | FOR_EACH_BINDING(DART_REGISTER_NATIVE)}); |
34 | } |
35 | |
36 | fml::RefPtr<ColorFilter> ColorFilter::Create() { |
37 | return fml::MakeRefCounted<ColorFilter>(); |
38 | } |
39 | |
40 | void ColorFilter::initMode(int color, int blend_mode) { |
41 | filter_ = SkColorFilters::Blend(static_cast<SkColor>(color), |
42 | static_cast<SkBlendMode>(blend_mode)); |
43 | } |
44 | |
45 | sk_sp<SkColorFilter> ColorFilter::MakeColorMatrixFilter255( |
46 | const float array[20]) { |
47 | float tmp[20]; |
48 | memcpy(tmp, array, sizeof(tmp)); |
49 | tmp[4] *= 1.0f / 255; |
50 | tmp[9] *= 1.0f / 255; |
51 | tmp[14] *= 1.0f / 255; |
52 | tmp[19] *= 1.0f / 255; |
53 | return SkColorFilters::Matrix(tmp); |
54 | } |
55 | |
56 | void ColorFilter::initMatrix(const tonic::Float32List& color_matrix) { |
57 | FML_CHECK(color_matrix.num_elements() == 20); |
58 | |
59 | filter_ = MakeColorMatrixFilter255(color_matrix.data()); |
60 | } |
61 | |
62 | void ColorFilter::initLinearToSrgbGamma() { |
63 | filter_ = SkColorFilters::LinearToSRGBGamma(); |
64 | } |
65 | |
66 | void ColorFilter::initSrgbToLinearGamma() { |
67 | filter_ = SkColorFilters::SRGBToLinearGamma(); |
68 | } |
69 | |
70 | ColorFilter::~ColorFilter() = default; |
71 | |
72 | } // namespace flutter |
73 | |