ColorTest.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include "include/core/SkColor.h"
  8. #include "include/core/SkTypes.h"
  9. #include "include/core/SkUnPreMultiply.h"
  10. #include "include/private/SkColorData.h"
  11. #include "include/utils/SkRandom.h"
  12. #include "src/core/SkMathPriv.h"
  13. #include "tests/Test.h"
  14. DEF_TEST(ColorPremul, reporter) {
  15. for (int a = 0; a <= 255; a++) {
  16. for (int x = 0; x <= 255; x++) {
  17. SkColor c0 = SkColorSetARGB(a, x, x, x);
  18. SkPMColor p0 = SkPreMultiplyColor(c0);
  19. SkColor c1 = SkUnPreMultiply::PMColorToColor(p0);
  20. SkPMColor p1 = SkPreMultiplyColor(c1);
  21. // we can't promise that c0 == c1, since c0 -> p0 is a many to one
  22. // function, however, we can promise that p0 -> c1 -> p1 : p0 == p1
  23. REPORTER_ASSERT(reporter, p0 == p1);
  24. {
  25. int ax = SkMulDiv255Ceiling(x, a);
  26. REPORTER_ASSERT(reporter, ax <= a);
  27. }
  28. }
  29. }
  30. }
  31. /**
  32. This test fails: SkFourByteInterp does *not* preserve opaque destinations.
  33. SkAlpha255To256 implemented as (alpha + 1) is faster than
  34. (alpha + (alpha >> 7)), but inaccurate, and Skia intends to phase it out.
  35. */
  36. DEF_TEST(ColorInterp, reporter) {
  37. SkRandom r;
  38. U8CPU a0 = 0;
  39. U8CPU a255 = 255;
  40. for (int i = 0; i < 200; i++) {
  41. SkColor colorSrc = r.nextU();
  42. SkColor colorDst = r.nextU();
  43. SkPMColor src = SkPreMultiplyColor(colorSrc);
  44. SkPMColor dst = SkPreMultiplyColor(colorDst);
  45. if (false) {
  46. REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a0) == dst);
  47. REPORTER_ASSERT(reporter, SkFourByteInterp(src, dst, a255) == src);
  48. }
  49. }
  50. }
  51. DEF_TEST(ColorFastIterp, reporter) {
  52. SkRandom r;
  53. U8CPU a0 = 0;
  54. U8CPU a255 = 255;
  55. for (int i = 0; i < 200; i++) {
  56. SkColor colorSrc = r.nextU();
  57. SkColor colorDst = r.nextU();
  58. SkPMColor src = SkPreMultiplyColor(colorSrc);
  59. SkPMColor dst = SkPreMultiplyColor(colorDst);
  60. REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a0) == dst);
  61. REPORTER_ASSERT(reporter, SkFastFourByteInterp(src, dst, a255) == src);
  62. }
  63. }