|
| 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 | +import 'dart:typed_data'; |
| 6 | +import 'dart:ui'; |
| 7 | + |
| 8 | +import 'package:test/test.dart'; |
| 9 | + |
| 10 | +const Color red = Color(0xFFAA0000); |
| 11 | +const Color green = Color(0xFF00AA00); |
| 12 | + |
| 13 | +const int greenRedColorBlend = 0xFF3131DB; |
| 14 | +const int greenRedColorBlendInverted = 0xFFCECE24; |
| 15 | +const int greenGreyscaled = 0xFF7A7A7A; |
| 16 | +const int greenInvertedGreyscaled = 0xFF858585; |
| 17 | + |
| 18 | +const int greenLinearToSrgbGamma = 0xFF00D500; |
| 19 | +const int greenLinearToSrgbGammaInverted = 0xFFFF2AFF; |
| 20 | + |
| 21 | +const int greenSrgbToLinearGamma = 0xFF006700; |
| 22 | +const int greenSrgbToLinearGammaInverted = 0xFFFF98FF; |
| 23 | + |
| 24 | +const List<double> greyscaleColorMatrix = <double>[ |
| 25 | + 0.2126, 0.7152, 0.0722, 0, 0, // |
| 26 | + 0.2126, 0.7152, 0.0722, 0, 0, // |
| 27 | + 0.2126, 0.7152, 0.0722, 0, 0, // |
| 28 | + 0, 0, 0, 1, 0, // |
| 29 | +]; |
| 30 | + |
| 31 | +void main() { |
| 32 | + Future<Uint32List> getBytesForPaint(Paint paint, {int width = 1, int height = 1}) async { |
| 33 | + final PictureRecorder recorder = PictureRecorder(); |
| 34 | + final Canvas recorderCanvas = Canvas(recorder); |
| 35 | + recorderCanvas.drawPaint(paint); |
| 36 | + final Picture picture = recorder.endRecording(); |
| 37 | + final Image image = await picture.toImage(width, height); |
| 38 | + final ByteData bytes = await image.toByteData(); |
| 39 | + |
| 40 | + expect(bytes.lengthInBytes, width * height * 4); |
| 41 | + return bytes.buffer.asUint32List(); |
| 42 | + } |
| 43 | + |
| 44 | + test('ColorFilter - mode', () async { |
| 45 | + final Paint paint = Paint() |
| 46 | + ..color = green |
| 47 | + ..colorFilter = ColorFilter.mode(red, BlendMode.color); |
| 48 | + |
| 49 | + Uint32List bytes = await getBytesForPaint(paint); |
| 50 | + expect(bytes[0], greenRedColorBlend); |
| 51 | + |
| 52 | + paint.invertColors = true; |
| 53 | + bytes = await getBytesForPaint(paint); |
| 54 | + expect(bytes[0], greenRedColorBlendInverted); |
| 55 | + }); |
| 56 | + |
| 57 | + test('ColorFilter - matrix', () async { |
| 58 | + final Paint paint = Paint() |
| 59 | + ..color = green |
| 60 | + ..colorFilter = ColorFilter.matrix(greyscaleColorMatrix); |
| 61 | + |
| 62 | + Uint32List bytes = await getBytesForPaint(paint); |
| 63 | + expect(bytes[0], greenGreyscaled); |
| 64 | + |
| 65 | + paint.invertColors = true; |
| 66 | + bytes = await getBytesForPaint(paint); |
| 67 | + expect(bytes[0], greenInvertedGreyscaled); |
| 68 | + }); |
| 69 | + |
| 70 | + test('ColorFilter - linearToSrgbGamma', () async { |
| 71 | + final Paint paint = Paint() |
| 72 | + ..color = green |
| 73 | + ..colorFilter = ColorFilter.linearToSrgbGamma(); |
| 74 | + |
| 75 | + Uint32List bytes = await getBytesForPaint(paint); |
| 76 | + expect(bytes[0], greenLinearToSrgbGamma); |
| 77 | + |
| 78 | + paint.invertColors = true; |
| 79 | + bytes = await getBytesForPaint(paint); |
| 80 | + expect(bytes[0], greenLinearToSrgbGammaInverted); |
| 81 | + }); |
| 82 | + |
| 83 | + test('ColorFilter - srgbToLinearGamma', () async { |
| 84 | + final Paint paint = Paint() |
| 85 | + ..color = green |
| 86 | + ..colorFilter = ColorFilter.srgbToLinearGamma(); |
| 87 | + |
| 88 | + Uint32List bytes = await getBytesForPaint(paint); |
| 89 | + expect(bytes[0], greenSrgbToLinearGamma); |
| 90 | + |
| 91 | + paint.invertColors = true; |
| 92 | + bytes = await getBytesForPaint(paint); |
| 93 | + expect(bytes[0], greenSrgbToLinearGammaInverted); |
| 94 | + }); |
| 95 | + |
| 96 | +} |
0 commit comments