From d83545e4904ba78c6bc7b82169e9be5070f64e32 Mon Sep 17 00:00:00 2001 From: Matt Sarett Date: Mon, 6 Mar 2017 11:11:23 -0500 Subject: [PATCH] Add SkColorSpace::isSRGB() BUG=skia: Change-Id: I124480a15e0c3cb01f4e22397c0a116f2d1c0d7d Reviewed-on: https://skia-review.googlesource.com/9239 Reviewed-by: Mike Klein Commit-Queue: Matt Sarett --- include/core/SkColorSpace.h | 16 ++++++++++++++++ src/core/SkColorSpace.cpp | 4 ++++ tests/ColorSpaceTest.cpp | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/include/core/SkColorSpace.h b/include/core/SkColorSpace.h index 978660b..c945703 100644 --- a/include/core/SkColorSpace.h +++ b/include/core/SkColorSpace.h @@ -106,6 +106,8 @@ public: /** * Returns true if the color space gamma is near enough to be approximated as sRGB. + * This includes the canonical sRGB transfer function as well as a 2.2f exponential + * transfer function. */ bool gammaCloseToSRGB() const; @@ -129,6 +131,20 @@ public: bool toXYZD50(SkMatrix44* toXYZD50) const; /** + * Returns true if the color space is sRGB. + * Returns false otherwise. + * + * This allows a little bit of tolerance, given that we might see small numerical error + * in some cases: converting ICC fixed point to float, converting white point to D50, + * rounding decisions on transfer function and matrix. + * + * This does not consider a 2.2f exponential transfer function to be sRGB. While these + * functions are similar (and it is sometimes useful to consider them together), this + * function checks for logical equality. + */ + bool isSRGB() const; + + /** * Returns nullptr on failure. Fails when we fallback to serializing ICC data and * the data is too large to serialize. */ diff --git a/src/core/SkColorSpace.cpp b/src/core/SkColorSpace.cpp index e059beb..3677fb6 100644 --- a/src/core/SkColorSpace.cpp +++ b/src/core/SkColorSpace.cpp @@ -299,6 +299,10 @@ bool SkColorSpace::toXYZD50(SkMatrix44* toXYZD50) const { return false; } +bool SkColorSpace::isSRGB() const { + return gSRGB == this || gSRGBNonLinearBlending == this; +} + /////////////////////////////////////////////////////////////////////////////////////////////////// sk_sp SkColorSpace_Base::makeWithoutFlags() { diff --git a/tests/ColorSpaceTest.cpp b/tests/ColorSpaceTest.cpp index 73a4980..059fa59 100644 --- a/tests/ColorSpaceTest.cpp +++ b/tests/ColorSpaceTest.cpp @@ -478,3 +478,23 @@ DEF_TEST(ColorSpace_MatrixHash, r) { REPORTER_ASSERT(r, *as_CSB(srgb)->toXYZD50() == *as_CSB(strange)->toXYZD50()); REPORTER_ASSERT(r, as_CSB(srgb)->toXYZD50Hash() == as_CSB(strange)->toXYZD50Hash()); } + +DEF_TEST(ColorSpace_IsSRGB, r) { + sk_sp srgb0 = SkColorSpace::MakeSRGB(); + sk_sp srgb1 = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, + SkColorSpace::kSRGB_Gamut, SkColorSpace::kNonLinearBlending_ColorSpaceFlag); + + SkColorSpaceTransferFn fn; + fn.fA = 1.0f; + fn.fB = 0.0f; + fn.fC = 0.0f; + fn.fD = 0.0f; + fn.fE = 0.0f; + fn.fF = 0.0f; + fn.fG = 2.2f; + sk_sp twoDotTwo = SkColorSpace::MakeRGB(fn, SkColorSpace::kSRGB_Gamut); + + REPORTER_ASSERT(r, srgb0->isSRGB()); + REPORTER_ASSERT(r, srgb1->isSRGB()); + REPORTER_ASSERT(r, !twoDotTwo->isSRGB()); +} -- 2.7.4