1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkTypes.h"
15 #include "tests/Test.h"
16
17 #include <array>
18 #include <cstddef>
19 #include <cstdint>
20
DEF_TEST(GetColor,reporter)21 DEF_TEST(GetColor, reporter) {
22 static const struct Rec {
23 SkColorType fColorType;
24 SkColor fInColor;
25 SkColor fOutColor;
26 } gRec[] = {
27 // todo: add some tests that involve alpha, so we exercise the
28 // unpremultiply aspect of getColor()
29 { kAlpha_8_SkColorType, 0xFF000000, 0xFF000000 },
30 { kAlpha_8_SkColorType, 0, 0 },
31 { kRGB_565_SkColorType, 0xFF00FF00, 0xFF00FF00 },
32 { kRGB_565_SkColorType, 0xFFFF00FF, 0xFFFF00FF },
33 { kN32_SkColorType, 0xFFFFFFFF, 0xFFFFFFFF },
34 { kN32_SkColorType, 0, 0 },
35 { kN32_SkColorType, 0xFF224466, 0xFF224466 },
36 };
37
38 // specify an area that doesn't touch (0,0) and may extend beyond the
39 // bitmap bounds (to test that we catch that in eraseArea
40 const SkColor initColor = 0xFF0000FF;
41 const SkIRect area = { 1, 1, 3, 3 };
42
43 for (size_t i = 0; i < std::size(gRec); i++) {
44 SkImageInfo info = SkImageInfo::Make(2, 2, gRec[i].fColorType,
45 kPremul_SkAlphaType);
46 SkBitmap bm;
47 uint32_t storage[4];
48 bm.installPixels(info, storage, info.minRowBytes());
49
50 bm.eraseColor(initColor);
51 bm.eraseArea(area, gRec[i].fInColor);
52
53 SkColor c = bm.getColor(1, 1);
54 REPORTER_ASSERT(reporter, c == gRec[i].fOutColor);
55 }
56 }
57