Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
dl_color.cc
Go to the documentation of this file.
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
6
7#include <algorithm>
8#include <cmath>
9
10namespace flutter {
11
12namespace {
13
14/// sRGB standard constants for transfer functions.
15/// See https://en.wikipedia.org/wiki/SRGB.
16constexpr double kSrgbGamma = 2.4;
17constexpr double kSrgbLinearThreshold = 0.04045;
18constexpr double kSrgbLinearSlope = 12.92;
19constexpr double kSrgbEncodedOffset = 0.055;
20constexpr double kSrgbEncodedDivisor = 1.055;
21constexpr double kSrgbLinearToEncodedThreshold = 0.0031308;
22
23/// sRGB electro-optical transfer function (gamma decode, gamma ~2.2 to linear).
24double srgbEOTF(double v) {
25 if (v <= kSrgbLinearThreshold) {
26 return v / kSrgbLinearSlope;
27 }
28 return std::pow((v + kSrgbEncodedOffset) / kSrgbEncodedDivisor, kSrgbGamma);
29}
30
31/// sRGB opto-electronic transfer function (linear to gamma encode).
32double srgbOETF(double v) {
33 if (v <= kSrgbLinearToEncodedThreshold) {
34 return v * kSrgbLinearSlope;
35 }
36 return kSrgbEncodedDivisor * std::pow(v, 1.0 / kSrgbGamma) -
37 kSrgbEncodedOffset;
38}
39
40/// sRGB EOTF extended to handle negative values (for extended sRGB).
41double srgbEOTFExtended(double v) {
42 return v < 0.0 ? -srgbEOTF(-v) : srgbEOTF(v);
43}
44
45/// sRGB OETF extended to handle negative values (for extended sRGB).
46double srgbOETFExtended(double v) {
47 return v < 0.0 ? -srgbOETF(-v) : srgbOETF(v);
48}
49
50/// Display P3 to sRGB linear 3x3 matrix.
51/// Both P3 and sRGB use the same D65 white point.
52/// P3 has wider primaries than sRGB, so converting P3 colors to sRGB
53/// can produce values outside [0,1] (extended sRGB).
54///
55/// Matrix derived from:
56/// M = sRGB_XYZ_to_RGB * P3_RGB_to_XYZ
57static constexpr double kP3ToSrgbLinear[9] = {
58 1.2249401, -0.2249402, 0.0, -0.0420569, 1.0420571,
59 0.0, -0.0196376, -0.0786507, 1.0982884,
60};
61
62/// Converts a Display P3 color (gamma-encoded) to extended sRGB
63/// (gamma-encoded). Steps: P3 gamma decode -> linear P3 -> linear sRGB (via 3x3
64/// matrix) -> sRGB gamma encode.
65DlColor p3ToExtendedSrgb(const DlColor& color) {
66 // Linearize P3 values (P3 uses same transfer function as sRGB).
67 double r_lin = srgbEOTFExtended(static_cast<double>(color.getRedF()));
68 double g_lin = srgbEOTFExtended(static_cast<double>(color.getGreenF()));
69 double b_lin = srgbEOTFExtended(static_cast<double>(color.getBlueF()));
70
71 // Apply 3x3 P3-to-sRGB matrix in linear space.
72 double r_srgb_lin = kP3ToSrgbLinear[0] * r_lin + kP3ToSrgbLinear[1] * g_lin +
73 kP3ToSrgbLinear[2] * b_lin;
74 double g_srgb_lin = kP3ToSrgbLinear[3] * r_lin + kP3ToSrgbLinear[4] * g_lin +
75 kP3ToSrgbLinear[5] * b_lin;
76 double b_srgb_lin = kP3ToSrgbLinear[6] * r_lin + kP3ToSrgbLinear[7] * g_lin +
77 kP3ToSrgbLinear[8] * b_lin;
78
79 // Gamma encode back to sRGB.
80 double r_out = srgbOETFExtended(r_srgb_lin);
81 double g_out = srgbOETFExtended(g_srgb_lin);
82 double b_out = srgbOETFExtended(b_srgb_lin);
83
84 return DlColor(color.getAlphaF(), static_cast<float>(r_out),
85 static_cast<float>(g_out), static_cast<float>(b_out),
87}
88
89} // namespace
90
92 switch (color_space_) {
94 switch (color_space) {
96 return *this;
98 return DlColor(alpha_, red_, green_, blue_,
101 FML_CHECK(false) << "not implemented";
102 return *this;
103 }
105 switch (color_space) {
107 return DlColor(alpha_, std::clamp(red_, 0.0f, 1.0f),
108 std::clamp(green_, 0.0f, 1.0f),
109 std::clamp(blue_, 0.0f, 1.0f), DlColorSpace::kSRGB);
111 return *this;
113 FML_CHECK(false) << "not implemented";
114 return *this;
115 }
117 switch (color_space) {
119 return p3ToExtendedSrgb(*this).withColorSpace(DlColorSpace::kSRGB);
121 return p3ToExtendedSrgb(*this);
123 return *this;
124 }
125 }
126}
127
128} // namespace flutter
#define FML_CHECK(condition)
Definition logging.h:104
DlColorSpace
Definition dl_color.h:13
flutter::DlColor DlColor
constexpr DlColor()
Definition dl_color.h:23
DlColor withColorSpace(DlColorSpace color_space) const
Definition dl_color.cc:91