Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to invert paint colors to be used for smart invert accessibility on iOS #6176

Merged
merged 4 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/ui/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ class Paint {
static const int _kMaskFilterIndex = 12;
static const int _kMaskFilterBlurStyleIndex = 13;
static const int _kMaskFilterSigmaIndex = 14;
static const int _kInvertColorIndex = 15;

static const int _kIsAntiAliasOffset = _kIsAntiAliasIndex << 2;
static const int _kColorOffset = _kColorIndex << 2;
Expand All @@ -1081,6 +1082,7 @@ class Paint {
static const int _kMaskFilterOffset = _kMaskFilterIndex << 2;
static const int _kMaskFilterBlurStyleOffset = _kMaskFilterBlurStyleIndex << 2;
static const int _kMaskFilterSigmaOffset = _kMaskFilterSigmaIndex << 2;
static const int _kInvertColorOffset = _kInvertColorIndex << 2;
// If you add more fields, remember to update _kDataByteCount.
static const int _kDataByteCount = 75;

Expand Down Expand Up @@ -1363,6 +1365,18 @@ class Paint {
}
}

/// Whether the colors of the image are inverted when drawn.
///
/// inverting the colors of an image applies a new color filter that will
/// override any user provided color filters. This is primarily intended to
/// be used for implementing smart invert on iOS.
bool get invertColors {
return _data.getInt32(_kInvertColorOffset, _kFakeHostEndian) == 1;
}
set invertColors(bool value) {
_data.setInt32(_kInvertColorOffset, value ? 1 : 0, _kFakeHostEndian);
}

@override
String toString() {
final StringBuffer result = new StringBuffer();
Expand Down Expand Up @@ -1411,8 +1425,12 @@ class Paint {
result.write('${semicolon}filterQuality: $filterQuality');
semicolon = '; ';
}
if (shader != null)
if (shader != null) {
result.write('${semicolon}shader: $shader');
semicolon = '; ';
}
if (invertColors)
result.write('${semicolon}invert: $invertColors');
result.write(')');
return result.toString();
}
Expand Down
8 changes: 8 additions & 0 deletions lib/ui/painting/paint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ constexpr int kColorFilterBlendModeIndex = 11;
constexpr int kMaskFilterIndex = 12;
constexpr int kMaskFilterBlurStyleIndex = 13;
constexpr int kMaskFilterSigmaIndex = 14;
constexpr int kInvertColorIndex = 15;
constexpr size_t kDataByteCount = 75; // 4 * (last index + 1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @Hixie - this calculation seems to be a bit off


// Indices for objects.
Expand Down Expand Up @@ -133,6 +134,13 @@ Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_data) {
paint_.setMaskFilter(SkMaskFilter::MakeBlur(blur_style, sigma));
break;
}

if (uint_data[kInvertColorIndex]) {
SkScalar colorMatrix[20] = {-1.0, 0, 0, 1.0, 0, 0, -1.0, 0, 1.0, 0,
0, 0, -1.0, 1.0, 0, 1.0, 1.0, 1.0, 1.0, 0};
paint_.setColorFilter(
SkColorFilter::MakeMatrixFilterRowMajor255(colorMatrix));
}
}

} // namespace blink
Expand Down