| 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 | #pragma once |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | #include <algorithm> |
| 9 | #include <array> |
| 10 | #include <cstdlib> |
| 11 | #include <ostream> |
| 12 | #include <type_traits> |
| 13 | |
| 14 | #include "impeller/geometry/scalar.h" |
| 15 | #include "impeller/geometry/type_traits.h" |
| 16 | |
| 17 | #define IMPELLER_FOR_EACH_BLEND_MODE(V) \ |
| 18 | V(Clear) \ |
| 19 | V(Source) \ |
| 20 | V(Destination) \ |
| 21 | V(SourceOver) \ |
| 22 | V(DestinationOver) \ |
| 23 | V(SourceIn) \ |
| 24 | V(DestinationIn) \ |
| 25 | V(SourceOut) \ |
| 26 | V(DestinationOut) \ |
| 27 | V(SourceATop) \ |
| 28 | V(DestinationATop) \ |
| 29 | V(Xor) \ |
| 30 | V(Plus) \ |
| 31 | V(Modulate) \ |
| 32 | V(Screen) \ |
| 33 | V(Overlay) \ |
| 34 | V(Darken) \ |
| 35 | V(Lighten) \ |
| 36 | V(ColorDodge) \ |
| 37 | V(ColorBurn) \ |
| 38 | V(HardLight) \ |
| 39 | V(SoftLight) \ |
| 40 | V(Difference) \ |
| 41 | V(Exclusion) \ |
| 42 | V(Multiply) \ |
| 43 | V(Hue) \ |
| 44 | V(Saturation) \ |
| 45 | V(Color) \ |
| 46 | V(Luminosity) |
| 47 | |
| 48 | namespace impeller { |
| 49 | |
| 50 | struct ColorHSB; |
| 51 | struct Vector4; |
| 52 | |
| 53 | enum class YUVColorSpace { kBT601LimitedRange, kBT601FullRange }; |
| 54 | |
| 55 | /// All blend modes assume that both the source (fragment output) and |
| 56 | /// destination (first color attachment) have colors with premultiplied alpha. |
| 57 | enum class BlendMode { |
| 58 | // The following blend modes are able to be used as pipeline blend modes or |
| 59 | // via `BlendFilterContents`. |
| 60 | kClear = 0, |
| 61 | kSource, |
| 62 | kDestination, |
| 63 | kSourceOver, |
| 64 | kDestinationOver, |
| 65 | kSourceIn, |
| 66 | kDestinationIn, |
| 67 | kSourceOut, |
| 68 | kDestinationOut, |
| 69 | kSourceATop, |
| 70 | kDestinationATop, |
| 71 | kXor, |
| 72 | kPlus, |
| 73 | kModulate, |
| 74 | |
| 75 | // The following blend modes use equations that are not available for |
| 76 | // pipelines on most graphics devices without extensions, and so they are |
| 77 | // only able to be used via `BlendFilterContents`. |
| 78 | kScreen, |
| 79 | kOverlay, |
| 80 | kDarken, |
| 81 | kLighten, |
| 82 | kColorDodge, |
| 83 | kColorBurn, |
| 84 | kHardLight, |
| 85 | kSoftLight, |
| 86 | kDifference, |
| 87 | kExclusion, |
| 88 | kMultiply, |
| 89 | kHue, |
| 90 | kSaturation, |
| 91 | kColor, |
| 92 | kLuminosity, |
| 93 | |
| 94 | kLast = kLuminosity, |
| 95 | }; |
| 96 | |
| 97 | const char* BlendModeToString(BlendMode blend_mode); |
| 98 | |
| 99 | /// 4x5 matrix for transforming the color and alpha components of a Bitmap. |
| 100 | /// |
| 101 | /// [ a, b, c, d, e, |
| 102 | /// f, g, h, i, j, |
| 103 | /// k, l, m, n, o, |
| 104 | /// p, q, r, s, t ] |
| 105 | /// |
| 106 | /// When applied to a color [R, G, B, A], the resulting color is computed as: |
| 107 | /// |
| 108 | /// R’ = a*R + b*G + c*B + d*A + e; |
| 109 | /// G’ = f*R + g*G + h*B + i*A + j; |
| 110 | /// B’ = k*R + l*G + m*B + n*A + o; |
| 111 | /// A’ = p*R + q*G + r*B + s*A + t; |
| 112 | /// |
| 113 | /// That resulting color [R’, G’, B’, A’] then has each channel clamped to the 0 |
| 114 | /// to 1 range. |
| 115 | struct ColorMatrix { |
| 116 | Scalar array[20]; |
| 117 | }; |
| 118 | |
| 119 | /** |
| 120 | * Represents a RGBA color |
| 121 | */ |
| 122 | struct Color { |
| 123 | /** |
| 124 | * The red color component (0 to 1) |
| 125 | */ |
| 126 | Scalar red = 0.0; |
| 127 | |
| 128 | /** |
| 129 | * The green color component (0 to 1) |
| 130 | */ |
| 131 | Scalar green = 0.0; |
| 132 | |
| 133 | /** |
| 134 | * The blue color component (0 to 1) |
| 135 | */ |
| 136 | Scalar blue = 0.0; |
| 137 | |
| 138 | /** |
| 139 | * The alpha component of the color (0 to 1) |
| 140 | */ |
| 141 | Scalar alpha = 0.0; |
| 142 | |
| 143 | constexpr Color() {} |
| 144 | |
| 145 | explicit Color(const ColorHSB& hsbColor); |
| 146 | |
| 147 | Color(const Vector4& value); |
| 148 | |
| 149 | constexpr Color(Scalar r, Scalar g, Scalar b, Scalar a) |
| 150 | : red(r), green(g), blue(b), alpha(a) {} |
| 151 | |
| 152 | static constexpr Color MakeRGBA8(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { |
| 153 | return Color( |
| 154 | static_cast<Scalar>(r) / 255.0f, static_cast<Scalar>(g) / 255.0f, |
| 155 | static_cast<Scalar>(b) / 255.0f, static_cast<Scalar>(a) / 255.0f); |
| 156 | } |
| 157 | |
| 158 | /// @brief Convert this color to a 32-bit representation. |
| 159 | static constexpr uint32_t ToIColor(Color color) { |
| 160 | return (((std::lround(lcpp_x: color.alpha * 255.0f) & 0xff) << 24) | |
| 161 | ((std::lround(lcpp_x: color.red * 255.0f) & 0xff) << 16) | |
| 162 | ((std::lround(lcpp_x: color.green * 255.0f) & 0xff) << 8) | |
| 163 | ((std::lround(lcpp_x: color.blue * 255.0f) & 0xff) << 0)) & |
| 164 | 0xFFFFFFFF; |
| 165 | } |
| 166 | |
| 167 | constexpr inline bool operator==(const Color& c) const { |
| 168 | return ScalarNearlyEqual(x: red, y: c.red) && ScalarNearlyEqual(x: green, y: c.green) && |
| 169 | ScalarNearlyEqual(x: blue, y: c.blue) && ScalarNearlyEqual(x: alpha, y: c.alpha); |
| 170 | } |
| 171 | |
| 172 | constexpr inline Color operator+(const Color& c) const { |
| 173 | return {red + c.red, green + c.green, blue + c.blue, alpha + c.alpha}; |
| 174 | } |
| 175 | |
| 176 | template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 177 | constexpr inline Color operator+(T value) const { |
| 178 | auto v = static_cast<Scalar>(value); |
| 179 | return {red + v, green + v, blue + v, alpha + v}; |
| 180 | } |
| 181 | |
| 182 | constexpr inline Color operator-(const Color& c) const { |
| 183 | return {red - c.red, green - c.green, blue - c.blue, alpha - c.alpha}; |
| 184 | } |
| 185 | |
| 186 | template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 187 | constexpr inline Color operator-(T value) const { |
| 188 | auto v = static_cast<Scalar>(value); |
| 189 | return {red - v, green - v, blue - v, alpha - v}; |
| 190 | } |
| 191 | |
| 192 | constexpr inline Color operator*(const Color& c) const { |
| 193 | return {red * c.red, green * c.green, blue * c.blue, alpha * c.alpha}; |
| 194 | } |
| 195 | |
| 196 | template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 197 | constexpr inline Color operator*(T value) const { |
| 198 | auto v = static_cast<Scalar>(value); |
| 199 | return {red * v, green * v, blue * v, alpha * v}; |
| 200 | } |
| 201 | |
| 202 | constexpr inline Color operator/(const Color& c) const { |
| 203 | return {red * c.red, green * c.green, blue * c.blue, alpha * c.alpha}; |
| 204 | } |
| 205 | |
| 206 | template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 207 | constexpr inline Color operator/(T value) const { |
| 208 | auto v = static_cast<Scalar>(value); |
| 209 | return {red / v, green / v, blue / v, alpha / v}; |
| 210 | } |
| 211 | |
| 212 | constexpr Color Premultiply() const { |
| 213 | return {red * alpha, green * alpha, blue * alpha, alpha}; |
| 214 | } |
| 215 | |
| 216 | constexpr Color Unpremultiply() const { |
| 217 | if (ScalarNearlyEqual(x: alpha, y: 0.0f)) { |
| 218 | return Color::BlackTransparent(); |
| 219 | } |
| 220 | return {red / alpha, green / alpha, blue / alpha, alpha}; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * @brief Return a color that is linearly interpolated between colors a |
| 225 | * and b, according to the value of t. |
| 226 | * |
| 227 | * @param a The lower color. |
| 228 | * @param b The upper color. |
| 229 | * @param t A value between 0.0f and 1.0f, inclusive. |
| 230 | * @return constexpr Color |
| 231 | */ |
| 232 | constexpr static Color Lerp(Color a, Color b, Scalar t) { |
| 233 | return a + (b - a) * t; |
| 234 | } |
| 235 | |
| 236 | constexpr Color Clamp01() const { |
| 237 | return Color(std::clamp(v: red, lo: 0.0f, hi: 1.0f), std::clamp(v: green, lo: 0.0f, hi: 1.0f), |
| 238 | std::clamp(v: blue, lo: 0.0f, hi: 1.0f), std::clamp(v: alpha, lo: 0.0f, hi: 1.0f)); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * @brief Convert to R8G8B8A8 representation. |
| 243 | * |
| 244 | * @return constexpr std::array<u_int8, 4> |
| 245 | */ |
| 246 | constexpr std::array<uint8_t, 4> ToR8G8B8A8() const { |
| 247 | uint8_t r = std::round(lcpp_x: red * 255.0f); |
| 248 | uint8_t g = std::round(lcpp_x: green * 255.0f); |
| 249 | uint8_t b = std::round(lcpp_x: blue * 255.0f); |
| 250 | uint8_t a = std::round(lcpp_x: alpha * 255.0f); |
| 251 | return {r, g, b, a}; |
| 252 | } |
| 253 | |
| 254 | static constexpr Color White() { return {1.0f, 1.0f, 1.0f, 1.0f}; } |
| 255 | |
| 256 | static constexpr Color Black() { return {0.0f, 0.0f, 0.0f, 1.0f}; } |
| 257 | |
| 258 | static constexpr Color WhiteTransparent() { return {1.0f, 1.0f, 1.0f, 0.0f}; } |
| 259 | |
| 260 | static constexpr Color BlackTransparent() { return {0.0f, 0.0f, 0.0f, 0.0f}; } |
| 261 | |
| 262 | static constexpr Color Red() { return {1.0f, 0.0f, 0.0f, 1.0f}; } |
| 263 | |
| 264 | static constexpr Color Green() { return {0.0f, 1.0f, 0.0f, 1.0f}; } |
| 265 | |
| 266 | static constexpr Color Blue() { return {0.0f, 0.0f, 1.0f, 1.0f}; } |
| 267 | |
| 268 | constexpr Color WithAlpha(Scalar new_alpha) const { |
| 269 | return {red, green, blue, new_alpha}; |
| 270 | } |
| 271 | |
| 272 | static constexpr Color AliceBlue() { |
| 273 | return {240.0f / 255.0f, 248.0f / 255.0f, 255.0f / 255.0f, 1.0f}; |
| 274 | } |
| 275 | |
| 276 | static constexpr Color AntiqueWhite() { |
| 277 | return {250.0f / 255.0f, 235.0f / 255.0f, 215.0f / 255.0f, 1.0f}; |
| 278 | } |
| 279 | |
| 280 | static constexpr Color Aqua() { |
| 281 | return {0.0f / 255.0f, 255.0f / 255.0f, 255.0f / 255.0f, 1.0f}; |
| 282 | } |
| 283 | |
| 284 | static constexpr Color AquaMarine() { |
| 285 | return {127.0f / 255.0f, 255.0f / 255.0f, 212.0f / 255.0f, 1.0f}; |
| 286 | } |
| 287 | |
| 288 | static constexpr Color Azure() { |
| 289 | return {240.0f / 255.0f, 255.0f / 255.0f, 255.0f / 255.0f, 1.0f}; |
| 290 | } |
| 291 | |
| 292 | static constexpr Color Beige() { |
| 293 | return {245.0f / 255.0f, 245.0f / 255.0f, 220.0f / 255.0f, 1.0f}; |
| 294 | } |
| 295 | |
| 296 | static constexpr Color Bisque() { |
| 297 | return {255.0f / 255.0f, 228.0f / 255.0f, 196.0f / 255.0f, 1.0f}; |
| 298 | } |
| 299 | |
| 300 | static constexpr Color BlanchedAlmond() { |
| 301 | return {255.0f / 255.0f, 235.0f / 255.0f, 205.0f / 255.0f, 1.0f}; |
| 302 | } |
| 303 | |
| 304 | static constexpr Color BlueViolet() { |
| 305 | return {138.0f / 255.0f, 43.0f / 255.0f, 226.0f / 255.0f, 1.0f}; |
| 306 | } |
| 307 | |
| 308 | static constexpr Color Brown() { |
| 309 | return {165.0f / 255.0f, 42.0f / 255.0f, 42.0f / 255.0f, 1.0f}; |
| 310 | } |
| 311 | |
| 312 | static constexpr Color BurlyWood() { |
| 313 | return {222.0f / 255.0f, 184.0f / 255.0f, 135.0f / 255.0f, 1.0f}; |
| 314 | } |
| 315 | |
| 316 | static constexpr Color CadetBlue() { |
| 317 | return {95.0f / 255.0f, 158.0f / 255.0f, 160.0f / 255.0f, 1.0f}; |
| 318 | } |
| 319 | |
| 320 | static constexpr Color Chartreuse() { |
| 321 | return {127.0f / 255.0f, 255.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 322 | } |
| 323 | |
| 324 | static constexpr Color Chocolate() { |
| 325 | return {210.0f / 255.0f, 105.0f / 255.0f, 30.0f / 255.0f, 1.0f}; |
| 326 | } |
| 327 | |
| 328 | static constexpr Color Coral() { |
| 329 | return {255.0f / 255.0f, 127.0f / 255.0f, 80.0f / 255.0f, 1.0f}; |
| 330 | } |
| 331 | |
| 332 | static constexpr Color CornflowerBlue() { |
| 333 | return {100.0f / 255.0f, 149.0f / 255.0f, 237.0f / 255.0f, 1.0f}; |
| 334 | } |
| 335 | |
| 336 | static constexpr Color Cornsilk() { |
| 337 | return {255.0f / 255.0f, 248.0f / 255.0f, 220.0f / 255.0f, 1.0f}; |
| 338 | } |
| 339 | |
| 340 | static constexpr Color Crimson() { |
| 341 | return {220.0f / 255.0f, 20.0f / 255.0f, 60.0f / 255.0f, 1.0f}; |
| 342 | } |
| 343 | |
| 344 | static constexpr Color Cyan() { |
| 345 | return {0.0f / 255.0f, 255.0f / 255.0f, 255.0f / 255.0f, 1.0f}; |
| 346 | } |
| 347 | |
| 348 | static constexpr Color DarkBlue() { |
| 349 | return {0.0f / 255.0f, 0.0f / 255.0f, 139.0f / 255.0f, 1.0f}; |
| 350 | } |
| 351 | |
| 352 | static constexpr Color DarkCyan() { |
| 353 | return {0.0f / 255.0f, 139.0f / 255.0f, 139.0f / 255.0f, 1.0f}; |
| 354 | } |
| 355 | |
| 356 | static constexpr Color DarkGoldenrod() { |
| 357 | return {184.0f / 255.0f, 134.0f / 255.0f, 11.0f / 255.0f, 1.0f}; |
| 358 | } |
| 359 | |
| 360 | static constexpr Color DarkGray() { |
| 361 | return {169.0f / 255.0f, 169.0f / 255.0f, 169.0f / 255.0f, 1.0f}; |
| 362 | } |
| 363 | |
| 364 | static constexpr Color DarkGreen() { |
| 365 | return {0.0f / 255.0f, 100.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 366 | } |
| 367 | |
| 368 | static constexpr Color DarkGrey() { |
| 369 | return {169.0f / 255.0f, 169.0f / 255.0f, 169.0f / 255.0f, 1.0f}; |
| 370 | } |
| 371 | |
| 372 | static constexpr Color DarkKhaki() { |
| 373 | return {189.0f / 255.0f, 183.0f / 255.0f, 107.0f / 255.0f, 1.0f}; |
| 374 | } |
| 375 | |
| 376 | static constexpr Color DarkMagenta() { |
| 377 | return {139.0f / 255.0f, 0.0f / 255.0f, 139.0f / 255.0f, 1.0f}; |
| 378 | } |
| 379 | |
| 380 | static constexpr Color DarkOliveGreen() { |
| 381 | return {85.0f / 255.0f, 107.0f / 255.0f, 47.0f / 255.0f, 1.0f}; |
| 382 | } |
| 383 | |
| 384 | static constexpr Color DarkOrange() { |
| 385 | return {255.0f / 255.0f, 140.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 386 | } |
| 387 | |
| 388 | static constexpr Color DarkOrchid() { |
| 389 | return {153.0f / 255.0f, 50.0f / 255.0f, 204.0f / 255.0f, 1.0f}; |
| 390 | } |
| 391 | |
| 392 | static constexpr Color DarkRed() { |
| 393 | return {139.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 394 | } |
| 395 | |
| 396 | static constexpr Color DarkSalmon() { |
| 397 | return {233.0f / 255.0f, 150.0f / 255.0f, 122.0f / 255.0f, 1.0f}; |
| 398 | } |
| 399 | |
| 400 | static constexpr Color DarkSeagreen() { |
| 401 | return {143.0f / 255.0f, 188.0f / 255.0f, 143.0f / 255.0f, 1.0f}; |
| 402 | } |
| 403 | |
| 404 | static constexpr Color DarkSlateBlue() { |
| 405 | return {72.0f / 255.0f, 61.0f / 255.0f, 139.0f / 255.0f, 1.0f}; |
| 406 | } |
| 407 | |
| 408 | static constexpr Color DarkSlateGray() { |
| 409 | return {47.0f / 255.0f, 79.0f / 255.0f, 79.0f / 255.0f, 1.0f}; |
| 410 | } |
| 411 | |
| 412 | static constexpr Color DarkSlateGrey() { |
| 413 | return {47.0f / 255.0f, 79.0f / 255.0f, 79.0f / 255.0f, 1.0f}; |
| 414 | } |
| 415 | |
| 416 | static constexpr Color DarkTurquoise() { |
| 417 | return {0.0f / 255.0f, 206.0f / 255.0f, 209.0f / 255.0f, 1.0f}; |
| 418 | } |
| 419 | |
| 420 | static constexpr Color DarkViolet() { |
| 421 | return {148.0f / 255.0f, 0.0f / 255.0f, 211.0f / 255.0f, 1.0f}; |
| 422 | } |
| 423 | |
| 424 | static constexpr Color DeepPink() { |
| 425 | return {255.0f / 255.0f, 20.0f / 255.0f, 147.0f / 255.0f, 1.0f}; |
| 426 | } |
| 427 | |
| 428 | static constexpr Color DeepSkyBlue() { |
| 429 | return {0.0f / 255.0f, 191.0f / 255.0f, 255.0f / 255.0f, 1.0f}; |
| 430 | } |
| 431 | |
| 432 | static constexpr Color DimGray() { |
| 433 | return {105.0f / 255.0f, 105.0f / 255.0f, 105.0f / 255.0f, 1.0f}; |
| 434 | } |
| 435 | |
| 436 | static constexpr Color DimGrey() { |
| 437 | return {105.0f / 255.0f, 105.0f / 255.0f, 105.0f / 255.0f, 1.0f}; |
| 438 | } |
| 439 | |
| 440 | static constexpr Color DodgerBlue() { |
| 441 | return {30.0f / 255.0f, 144.0f / 255.0f, 255.0f / 255.0f, 1.0f}; |
| 442 | } |
| 443 | |
| 444 | static constexpr Color Firebrick() { |
| 445 | return {178.0f / 255.0f, 34.0f / 255.0f, 34.0f / 255.0f, 1.0f}; |
| 446 | } |
| 447 | |
| 448 | static constexpr Color FloralWhite() { |
| 449 | return {255.0f / 255.0f, 250.0f / 255.0f, 240.0f / 255.0f, 1.0f}; |
| 450 | } |
| 451 | |
| 452 | static constexpr Color ForestGreen() { |
| 453 | return {34.0f / 255.0f, 139.0f / 255.0f, 34.0f / 255.0f, 1.0f}; |
| 454 | } |
| 455 | |
| 456 | static constexpr Color Fuchsia() { |
| 457 | return {255.0f / 255.0f, 0.0f / 255.0f, 255.0f / 255.0f, 1.0f}; |
| 458 | } |
| 459 | |
| 460 | static constexpr Color Gainsboro() { |
| 461 | return {220.0f / 255.0f, 220.0f / 255.0f, 220.0f / 255.0f, 1.0f}; |
| 462 | } |
| 463 | |
| 464 | static constexpr Color Ghostwhite() { |
| 465 | return {248.0f / 255.0f, 248.0f / 255.0f, 255.0f / 255.0f, 1.0f}; |
| 466 | } |
| 467 | |
| 468 | static constexpr Color Gold() { |
| 469 | return {255.0f / 255.0f, 215.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 470 | } |
| 471 | |
| 472 | static constexpr Color Goldenrod() { |
| 473 | return {218.0f / 255.0f, 165.0f / 255.0f, 32.0f / 255.0f, 1.0f}; |
| 474 | } |
| 475 | |
| 476 | static constexpr Color Gray() { |
| 477 | return {128.0f / 255.0f, 128.0f / 255.0f, 128.0f / 255.0f, 1.0f}; |
| 478 | } |
| 479 | |
| 480 | static constexpr Color GreenYellow() { |
| 481 | return {173.0f / 255.0f, 255.0f / 255.0f, 47.0f / 255.0f, 1.0f}; |
| 482 | } |
| 483 | |
| 484 | static constexpr Color Grey() { |
| 485 | return {128.0f / 255.0f, 128.0f / 255.0f, 128.0f / 255.0f, 1.0f}; |
| 486 | } |
| 487 | |
| 488 | static constexpr Color Honeydew() { |
| 489 | return {240.0f / 255.0f, 255.0f / 255.0f, 240.0f / 255.0f, 1.0f}; |
| 490 | } |
| 491 | |
| 492 | static constexpr Color HotPink() { |
| 493 | return {255.0f / 255.0f, 105.0f / 255.0f, 180.0f / 255.0f, 1.0f}; |
| 494 | } |
| 495 | |
| 496 | static constexpr Color IndianRed() { |
| 497 | return {205.0f / 255.0f, 92.0f / 255.0f, 92.0f / 255.0f, 1.0f}; |
| 498 | } |
| 499 | |
| 500 | static constexpr Color Indigo() { |
| 501 | return {75.0f / 255.0f, 0.0f / 255.0f, 130.0f / 255.0f, 1.0f}; |
| 502 | } |
| 503 | |
| 504 | static constexpr Color Ivory() { |
| 505 | return {255.0f / 255.0f, 255.0f / 255.0f, 240.0f / 255.0f, 1.0f}; |
| 506 | } |
| 507 | |
| 508 | static constexpr Color Khaki() { |
| 509 | return {240.0f / 255.0f, 230.0f / 255.0f, 140.0f / 255.0f, 1.0f}; |
| 510 | } |
| 511 | |
| 512 | static constexpr Color Lavender() { |
| 513 | return {230.0f / 255.0f, 230.0f / 255.0f, 250.0f / 255.0f, 1.0f}; |
| 514 | } |
| 515 | |
| 516 | static constexpr Color LavenderBlush() { |
| 517 | return {255.0f / 255.0f, 240.0f / 255.0f, 245.0f / 255.0f, 1.0f}; |
| 518 | } |
| 519 | |
| 520 | static constexpr Color LawnGreen() { |
| 521 | return {124.0f / 255.0f, 252.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 522 | } |
| 523 | |
| 524 | static constexpr Color LemonChiffon() { |
| 525 | return {255.0f / 255.0f, 250.0f / 255.0f, 205.0f / 255.0f, 1.0f}; |
| 526 | } |
| 527 | |
| 528 | static constexpr Color LightBlue() { |
| 529 | return {173.0f / 255.0f, 216.0f / 255.0f, 230.0f / 255.0f, 1.0f}; |
| 530 | } |
| 531 | |
| 532 | static constexpr Color LightCoral() { |
| 533 | return {240.0f / 255.0f, 128.0f / 255.0f, 128.0f / 255.0f, 1.0f}; |
| 534 | } |
| 535 | |
| 536 | static constexpr Color LightCyan() { |
| 537 | return {224.0f / 255.0f, 255.0f / 255.0f, 255.0f / 255.0f, 1.0f}; |
| 538 | } |
| 539 | |
| 540 | static constexpr Color LightGoldenrodYellow() { |
| 541 | return {50.0f / 255.0f, 250.0f / 255.0f, 210.0f / 255.0f, 1.0f}; |
| 542 | } |
| 543 | |
| 544 | static constexpr Color LightGray() { |
| 545 | return {211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f}; |
| 546 | } |
| 547 | |
| 548 | static constexpr Color LightGreen() { |
| 549 | return {144.0f / 255.0f, 238.0f / 255.0f, 144.0f / 255.0f, 1.0f}; |
| 550 | } |
| 551 | |
| 552 | static constexpr Color LightGrey() { |
| 553 | return {211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f}; |
| 554 | } |
| 555 | |
| 556 | static constexpr Color LightPink() { |
| 557 | return {255.0f / 255.0f, 182.0f / 255.0f, 193.0f / 255.0f, 1.0f}; |
| 558 | } |
| 559 | |
| 560 | static constexpr Color LightSalmon() { |
| 561 | return {255.0f / 255.0f, 160.0f / 255.0f, 122.0f / 255.0f, 1.0f}; |
| 562 | } |
| 563 | |
| 564 | static constexpr Color LightSeaGreen() { |
| 565 | return {32.0f / 255.0f, 178.0f / 255.0f, 170.0f / 255.0f, 1.0f}; |
| 566 | } |
| 567 | |
| 568 | static constexpr Color LightSkyBlue() { |
| 569 | return {135.0f / 255.0f, 206.0f / 255.0f, 250.0f / 255.0f, 1.0f}; |
| 570 | } |
| 571 | |
| 572 | static constexpr Color LightSlateGray() { |
| 573 | return {119.0f / 255.0f, 136.0f / 255.0f, 153.0f / 255.0f, 1.0f}; |
| 574 | } |
| 575 | |
| 576 | static constexpr Color LightSlateGrey() { |
| 577 | return {119.0f / 255.0f, 136.0f / 255.0f, 153.0f / 255.0f, 1.0f}; |
| 578 | } |
| 579 | |
| 580 | static constexpr Color LightSteelBlue() { |
| 581 | return {176.0f / 255.0f, 196.0f / 255.0f, 222.0f / 255.0f, 1.0f}; |
| 582 | } |
| 583 | |
| 584 | static constexpr Color LightYellow() { |
| 585 | return {255.0f / 255.0f, 255.0f / 255.0f, 224.0f / 255.0f, 1.0f}; |
| 586 | } |
| 587 | |
| 588 | static constexpr Color Lime() { |
| 589 | return {0.0f / 255.0f, 255.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 590 | } |
| 591 | |
| 592 | static constexpr Color LimeGreen() { |
| 593 | return {50.0f / 255.0f, 205.0f / 255.0f, 50.0f / 255.0f, 1.0f}; |
| 594 | } |
| 595 | |
| 596 | static constexpr Color Linen() { |
| 597 | return {250.0f / 255.0f, 240.0f / 255.0f, 230.0f / 255.0f, 1.0f}; |
| 598 | } |
| 599 | |
| 600 | static constexpr Color Magenta() { |
| 601 | return {255.0f / 255.0f, 0.0f / 255.0f, 255.0f / 255.0f, 1.0f}; |
| 602 | } |
| 603 | |
| 604 | static constexpr Color Maroon() { |
| 605 | return {128.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 606 | } |
| 607 | |
| 608 | static constexpr Color MediumAquamarine() { |
| 609 | return {102.0f / 255.0f, 205.0f / 255.0f, 170.0f / 255.0f, 1.0f}; |
| 610 | } |
| 611 | |
| 612 | static constexpr Color MediumBlue() { |
| 613 | return {0.0f / 255.0f, 0.0f / 255.0f, 205.0f / 255.0f, 1.0f}; |
| 614 | } |
| 615 | |
| 616 | static constexpr Color MediumOrchid() { |
| 617 | return {186.0f / 255.0f, 85.0f / 255.0f, 211.0f / 255.0f, 1.0f}; |
| 618 | } |
| 619 | |
| 620 | static constexpr Color MediumPurple() { |
| 621 | return {147.0f / 255.0f, 112.0f / 255.0f, 219.0f / 255.0f, 1.0f}; |
| 622 | } |
| 623 | |
| 624 | static constexpr Color MediumSeagreen() { |
| 625 | return {60.0f / 255.0f, 179.0f / 255.0f, 113.0f / 255.0f, 1.0f}; |
| 626 | } |
| 627 | |
| 628 | static constexpr Color MediumSlateBlue() { |
| 629 | return {123.0f / 255.0f, 104.0f / 255.0f, 238.0f / 255.0f, 1.0f}; |
| 630 | } |
| 631 | |
| 632 | static constexpr Color MediumSpringGreen() { |
| 633 | return {0.0f / 255.0f, 250.0f / 255.0f, 154.0f / 255.0f, 1.0f}; |
| 634 | } |
| 635 | |
| 636 | static constexpr Color MediumTurquoise() { |
| 637 | return {72.0f / 255.0f, 209.0f / 255.0f, 204.0f / 255.0f, 1.0f}; |
| 638 | } |
| 639 | |
| 640 | static constexpr Color MediumVioletRed() { |
| 641 | return {199.0f / 255.0f, 21.0f / 255.0f, 133.0f / 255.0f, 1.0f}; |
| 642 | } |
| 643 | |
| 644 | static constexpr Color MidnightBlue() { |
| 645 | return {25.0f / 255.0f, 25.0f / 255.0f, 112.0f / 255.0f, 1.0f}; |
| 646 | } |
| 647 | |
| 648 | static constexpr Color MintCream() { |
| 649 | return {245.0f / 255.0f, 255.0f / 255.0f, 250.0f / 255.0f, 1.0f}; |
| 650 | } |
| 651 | |
| 652 | static constexpr Color MistyRose() { |
| 653 | return {255.0f / 255.0f, 228.0f / 255.0f, 225.0f / 255.0f, 1.0f}; |
| 654 | } |
| 655 | |
| 656 | static constexpr Color Moccasin() { |
| 657 | return {255.0f / 255.0f, 228.0f / 255.0f, 181.0f / 255.0f, 1.0f}; |
| 658 | } |
| 659 | |
| 660 | static constexpr Color NavajoWhite() { |
| 661 | return {255.0f / 255.0f, 222.0f / 255.0f, 173.0f / 255.0f, 1.0f}; |
| 662 | } |
| 663 | |
| 664 | static constexpr Color Navy() { |
| 665 | return {0.0f / 255.0f, 0.0f / 255.0f, 128.0f / 255.0f, 1.0f}; |
| 666 | } |
| 667 | |
| 668 | static constexpr Color OldLace() { |
| 669 | return {253.0f / 255.0f, 245.0f / 255.0f, 230.0f / 255.0f, 1.0f}; |
| 670 | } |
| 671 | |
| 672 | static constexpr Color Olive() { |
| 673 | return {128.0f / 255.0f, 128.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 674 | } |
| 675 | |
| 676 | static constexpr Color OliveDrab() { |
| 677 | return {107.0f / 255.0f, 142.0f / 255.0f, 35.0f / 255.0f, 1.0f}; |
| 678 | } |
| 679 | |
| 680 | static constexpr Color Orange() { |
| 681 | return {255.0f / 255.0f, 165.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 682 | } |
| 683 | |
| 684 | static constexpr Color OrangeRed() { |
| 685 | return {255.0f / 255.0f, 69.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 686 | } |
| 687 | |
| 688 | static constexpr Color Orchid() { |
| 689 | return {218.0f / 255.0f, 112.0f / 255.0f, 214.0f / 255.0f, 1.0f}; |
| 690 | } |
| 691 | |
| 692 | static constexpr Color PaleGoldenrod() { |
| 693 | return {238.0f / 255.0f, 232.0f / 255.0f, 170.0f / 255.0f, 1.0f}; |
| 694 | } |
| 695 | |
| 696 | static constexpr Color PaleGreen() { |
| 697 | return {152.0f / 255.0f, 251.0f / 255.0f, 152.0f / 255.0f, 1.0f}; |
| 698 | } |
| 699 | |
| 700 | static constexpr Color PaleTurquoise() { |
| 701 | return {175.0f / 255.0f, 238.0f / 255.0f, 238.0f / 255.0f, 1.0f}; |
| 702 | } |
| 703 | |
| 704 | static constexpr Color PaleVioletRed() { |
| 705 | return {219.0f / 255.0f, 112.0f / 255.0f, 147.0f / 255.0f, 1.0f}; |
| 706 | } |
| 707 | |
| 708 | static constexpr Color PapayaWhip() { |
| 709 | return {255.0f / 255.0f, 239.0f / 255.0f, 213.0f / 255.0f, 1.0f}; |
| 710 | } |
| 711 | |
| 712 | static constexpr Color Peachpuff() { |
| 713 | return {255.0f / 255.0f, 218.0f / 255.0f, 185.0f / 255.0f, 1.0f}; |
| 714 | } |
| 715 | |
| 716 | static constexpr Color Peru() { |
| 717 | return {205.0f / 255.0f, 133.0f / 255.0f, 63.0f / 255.0f, 1.0f}; |
| 718 | } |
| 719 | |
| 720 | static constexpr Color Pink() { |
| 721 | return {255.0f / 255.0f, 192.0f / 255.0f, 203.0f / 255.0f, 1.0f}; |
| 722 | } |
| 723 | |
| 724 | static constexpr Color Plum() { |
| 725 | return {221.0f / 255.0f, 160.0f / 255.0f, 221.0f / 255.0f, 1.0f}; |
| 726 | } |
| 727 | |
| 728 | static constexpr Color PowderBlue() { |
| 729 | return {176.0f / 255.0f, 224.0f / 255.0f, 230.0f / 255.0f, 1.0f}; |
| 730 | } |
| 731 | |
| 732 | static constexpr Color Purple() { |
| 733 | return {128.0f / 255.0f, 0.0f / 255.0f, 128.0f / 255.0f, 1.0f}; |
| 734 | } |
| 735 | |
| 736 | static constexpr Color RosyBrown() { |
| 737 | return {188.0f / 255.0f, 143.0f / 255.0f, 143.0f / 255.0f, 1.0f}; |
| 738 | } |
| 739 | |
| 740 | static constexpr Color RoyalBlue() { |
| 741 | return {65.0f / 255.0f, 105.0f / 255.0f, 225.0f / 255.0f, 1.0f}; |
| 742 | } |
| 743 | |
| 744 | static constexpr Color SaddleBrown() { |
| 745 | return {139.0f / 255.0f, 69.0f / 255.0f, 19.0f / 255.0f, 1.0f}; |
| 746 | } |
| 747 | |
| 748 | static constexpr Color Salmon() { |
| 749 | return {250.0f / 255.0f, 128.0f / 255.0f, 114.0f / 255.0f, 1.0f}; |
| 750 | } |
| 751 | |
| 752 | static constexpr Color SandyBrown() { |
| 753 | return {244.0f / 255.0f, 164.0f / 255.0f, 96.0f / 255.0f, 1.0f}; |
| 754 | } |
| 755 | |
| 756 | static constexpr Color Seagreen() { |
| 757 | return {46.0f / 255.0f, 139.0f / 255.0f, 87.0f / 255.0f, 1.0f}; |
| 758 | } |
| 759 | |
| 760 | static constexpr Color Seashell() { |
| 761 | return {255.0f / 255.0f, 245.0f / 255.0f, 238.0f / 255.0f, 1.0f}; |
| 762 | } |
| 763 | |
| 764 | static constexpr Color Sienna() { |
| 765 | return {160.0f / 255.0f, 82.0f / 255.0f, 45.0f / 255.0f, 1.0f}; |
| 766 | } |
| 767 | |
| 768 | static constexpr Color Silver() { |
| 769 | return {192.0f / 255.0f, 192.0f / 255.0f, 192.0f / 255.0f, 1.0f}; |
| 770 | } |
| 771 | |
| 772 | static constexpr Color SkyBlue() { |
| 773 | return {135.0f / 255.0f, 206.0f / 255.0f, 235.0f / 255.0f, 1.0f}; |
| 774 | } |
| 775 | |
| 776 | static constexpr Color SlateBlue() { |
| 777 | return {106.0f / 255.0f, 90.0f / 255.0f, 205.0f / 255.0f, 1.0f}; |
| 778 | } |
| 779 | |
| 780 | static constexpr Color SlateGray() { |
| 781 | return {112.0f / 255.0f, 128.0f / 255.0f, 144.0f / 255.0f, 1.0f}; |
| 782 | } |
| 783 | |
| 784 | static constexpr Color SlateGrey() { |
| 785 | return {112.0f / 255.0f, 128.0f / 255.0f, 144.0f / 255.0f, 1.0f}; |
| 786 | } |
| 787 | |
| 788 | static constexpr Color Snow() { |
| 789 | return {255.0f / 255.0f, 250.0f / 255.0f, 250.0f / 255.0f, 1.0f}; |
| 790 | } |
| 791 | |
| 792 | static constexpr Color SpringGreen() { |
| 793 | return {0.0f / 255.0f, 255.0f / 255.0f, 127.0f / 255.0f, 1.0f}; |
| 794 | } |
| 795 | |
| 796 | static constexpr Color SteelBlue() { |
| 797 | return {70.0f / 255.0f, 130.0f / 255.0f, 180.0f / 255.0f, 1.0f}; |
| 798 | } |
| 799 | |
| 800 | static constexpr Color Tan() { |
| 801 | return {210.0f / 255.0f, 180.0f / 255.0f, 140.0f / 255.0f, 1.0f}; |
| 802 | } |
| 803 | |
| 804 | static constexpr Color Teal() { |
| 805 | return {0.0f / 255.0f, 128.0f / 255.0f, 128.0f / 255.0f, 1.0f}; |
| 806 | } |
| 807 | |
| 808 | static constexpr Color Thistle() { |
| 809 | return {216.0f / 255.0f, 191.0f / 255.0f, 216.0f / 255.0f, 1.0f}; |
| 810 | } |
| 811 | |
| 812 | static constexpr Color Tomato() { |
| 813 | return {255.0f / 255.0f, 99.0f / 255.0f, 71.0f / 255.0f, 1.0f}; |
| 814 | } |
| 815 | |
| 816 | static constexpr Color Turquoise() { |
| 817 | return {64.0f / 255.0f, 224.0f / 255.0f, 208.0f / 255.0f, 1.0f}; |
| 818 | } |
| 819 | |
| 820 | static constexpr Color Violet() { |
| 821 | return {238.0f / 255.0f, 130.0f / 255.0f, 238.0f / 255.0f, 1.0f}; |
| 822 | } |
| 823 | |
| 824 | static constexpr Color Wheat() { |
| 825 | return {245.0f / 255.0f, 222.0f / 255.0f, 179.0f / 255.0f, 1.0f}; |
| 826 | } |
| 827 | |
| 828 | static constexpr Color Whitesmoke() { |
| 829 | return {245.0f / 255.0f, 245.0f / 255.0f, 245.0f / 255.0f, 1.0f}; |
| 830 | } |
| 831 | |
| 832 | static constexpr Color Yellow() { |
| 833 | return {255.0f / 255.0f, 255.0f / 255.0f, 0.0f / 255.0f, 1.0f}; |
| 834 | } |
| 835 | |
| 836 | static constexpr Color YellowGreen() { |
| 837 | return {154.0f / 255.0f, 205.0f / 255.0f, 50.0f / 255.0f, 1.0f}; |
| 838 | } |
| 839 | |
| 840 | static Color Random() { |
| 841 | return { |
| 842 | static_cast<Scalar>((std::rand() % 255) / 255.0f), // |
| 843 | static_cast<Scalar>((std::rand() % 255) / 255.0f), // |
| 844 | static_cast<Scalar>((std::rand() % 255) / 255.0f), // |
| 845 | 1.0f // |
| 846 | }; |
| 847 | } |
| 848 | |
| 849 | /// @brief Blends an unpremultiplied destination color into a given |
| 850 | /// unpremultiplied source color to form a new unpremultiplied color. |
| 851 | /// |
| 852 | /// If either the source or destination are premultiplied, the result |
| 853 | /// will be incorrect. |
| 854 | Color Blend(Color source, BlendMode blend_mode) const; |
| 855 | |
| 856 | /// @brief A color filter that transforms colors through a 4x5 color matrix. |
| 857 | /// |
| 858 | /// This filter can be used to change the saturation of pixels, convert |
| 859 | /// from YUV to RGB, etc. |
| 860 | /// |
| 861 | /// Each channel of the output color is clamped to the 0 to 1 range. |
| 862 | /// |
| 863 | /// @see `ColorMatrix` |
| 864 | Color ApplyColorMatrix(const ColorMatrix& color_matrix) const; |
| 865 | |
| 866 | /// @brief Convert the color from linear space to sRGB space. |
| 867 | /// |
| 868 | /// The color is assumed to be unpremultiplied. If the color is |
| 869 | /// premultipled, the conversion output will be incorrect. |
| 870 | Color LinearToSRGB() const; |
| 871 | |
| 872 | /// @brief Convert the color from sRGB space to linear space. |
| 873 | /// |
| 874 | /// The color is assumed to be unpremultiplied. If the color is |
| 875 | /// premultipled, the conversion output will be incorrect. |
| 876 | Color SRGBToLinear() const; |
| 877 | |
| 878 | constexpr bool IsTransparent() const { return alpha == 0.0f; } |
| 879 | |
| 880 | constexpr bool IsOpaque() const { return alpha == 1.0f; } |
| 881 | }; |
| 882 | |
| 883 | template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 884 | constexpr inline Color operator+(T value, const Color& c) { |
| 885 | return c + static_cast<Scalar>(value); |
| 886 | } |
| 887 | |
| 888 | template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 889 | constexpr inline Color operator-(T value, const Color& c) { |
| 890 | auto v = static_cast<Scalar>(value); |
| 891 | return {v - c.red, v - c.green, v - c.blue, v - c.alpha}; |
| 892 | } |
| 893 | |
| 894 | template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 895 | constexpr inline Color operator*(T value, const Color& c) { |
| 896 | return c * static_cast<Scalar>(value); |
| 897 | } |
| 898 | |
| 899 | template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>> |
| 900 | constexpr inline Color operator/(T value, const Color& c) { |
| 901 | auto v = static_cast<Scalar>(value); |
| 902 | return {v / c.red, v / c.green, v / c.blue, v / c.alpha}; |
| 903 | } |
| 904 | |
| 905 | std::string ColorToString(const Color& color); |
| 906 | |
| 907 | /** |
| 908 | * Represents a color by its constituent hue, saturation, brightness and alpha |
| 909 | */ |
| 910 | struct ColorHSB { |
| 911 | /** |
| 912 | * The hue of the color (0 to 1) |
| 913 | */ |
| 914 | Scalar hue; |
| 915 | |
| 916 | /** |
| 917 | * The saturation of the color (0 to 1) |
| 918 | */ |
| 919 | Scalar saturation; |
| 920 | |
| 921 | /** |
| 922 | * The brightness of the color (0 to 1) |
| 923 | */ |
| 924 | Scalar brightness; |
| 925 | |
| 926 | /** |
| 927 | * The alpha of the color (0 to 1) |
| 928 | */ |
| 929 | Scalar alpha; |
| 930 | |
| 931 | constexpr ColorHSB(Scalar h, Scalar s, Scalar b, Scalar a) |
| 932 | : hue(h), saturation(s), brightness(b), alpha(a) {} |
| 933 | |
| 934 | static ColorHSB FromRGB(Color rgb); |
| 935 | |
| 936 | Color ToRGBA() const; |
| 937 | }; |
| 938 | |
| 939 | static_assert(sizeof(Color) == 4 * sizeof(Scalar)); |
| 940 | |
| 941 | } // namespace impeller |
| 942 | |
| 943 | namespace std { |
| 944 | |
| 945 | inline std::ostream& operator<<(std::ostream& out, const impeller::Color& c) { |
| 946 | out << "(" << c.red << ", " << c.green << ", " << c.blue << ", " << c.alpha |
| 947 | << ")" ; |
| 948 | return out; |
| 949 | } |
| 950 | |
| 951 | } // namespace std |
| 952 | |