chromium/third_party/blink/renderer/platform/graphics/color.cc

/*
 * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "third_party/blink/renderer/platform/graphics/color.h"

#include <math.h>

#include <optional>
#include <tuple>

#include "base/check_op.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/renderer/platform/geometry/blend.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/string_view.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/color_conversions.h"

namespace blink {

const Color Color::kBlack =;
const Color Color::kWhite =;
const Color Color::kDarkGray =;
const Color Color::kGray =;
const Color Color::kLightGray =;
const Color Color::kTransparent =;

namespace {

const RGBA32 kLightenedBlack =;
const RGBA32 kDarkenedWhite =;
// For lch/oklch colors, the value of chroma underneath which the color is
// considered to be "achromatic", relevant for color conversions.
// https://www.w3.org/TR/css-color-4/#lab-to-lch
const float kAchromaticChromaThreshold =;

const int kCStartAlpha =;     // 60%
const int kCEndAlpha =;       // 80%;
const int kCAlphaIncrement =;  // Increments in between.

int BlendComponent(int c, int a) {}

// originally moved here from the CSS parser
template <typename CharacterType>
inline bool ParseHexColorInternal(const CharacterType* name,
                                  unsigned length,
                                  Color& color) {}

inline const NamedColor* FindNamedColor(const String& name) {}

constexpr int RedChannel(RGBA32 color) {}

constexpr int GreenChannel(RGBA32 color) {}

constexpr int BlueChannel(RGBA32 color) {}

constexpr int AlphaChannel(RGBA32 color) {}

float AngleToUnitCircleDegrees(float angle) {}
}  // namespace

// The color parameters will use 16 bytes (for 4 floats). Ensure that the
// remaining parameters fit into another 4 bytes (or 8 bytes, on Windows)
#if BUILDFLAG(IS_WIN)
static_assert(sizeof(Color) <= 24, "blink::Color should be <= 24 bytes.");
#else
static_assert;
#endif

Color::Color(int r, int g, int b) {}

Color::Color(int r, int g, int b, int a) {}

// static
Color Color::FromRGBALegacy(std::optional<int> r,
                            std::optional<int> g,
                            std::optional<int> b,
                            std::optional<int> a) {}

// static
Color Color::FromColorSpace(ColorSpace color_space,
                            std::optional<float> param0,
                            std::optional<float> param1,
                            std::optional<float> param2,
                            std::optional<float> alpha) {}

// static
Color Color::FromHSLA(std::optional<float> h,
                      std::optional<float> s,
                      std::optional<float> l,
                      std::optional<float> a) {}

// static
Color Color::FromHWBA(std::optional<float> h,
                      std::optional<float> w,
                      std::optional<float> b,
                      std::optional<float> a) {}

// static
Color Color::FromColorMix(Color::ColorSpace interpolation_space,
                          std::optional<HueInterpolationMethod> hue_method,
                          Color color1,
                          Color color2,
                          float percentage,
                          float alpha_multiplier) {}

// static
float Color::HueInterpolation(float value1,
                              float value2,
                              float percentage,
                              Color::HueInterpolationMethod hue_method) {}

namespace {}  // namespace

void Color::CarryForwardAnalogousMissingComponents(
    Color color,
    Color::ColorSpace prev_color_space) {}

// static
bool Color::SubstituteMissingParameters(Color& color1, Color& color2) {}

// static
Color Color::InterpolateColors(Color::ColorSpace interpolation_space,
                               std::optional<HueInterpolationMethod> hue_method,
                               Color color1,
                               Color color2,
                               float percentage) {}

std::tuple<float, float, float> Color::ExportAsXYZD50Floats() const {}

// https://www.w3.org/TR/css-color-4/#missing:
// "[Except for interpolations] a missing component behaves as a zero value, in
// the appropriate unit for that component: 0, 0%, or 0deg. This includes
// rendering the color directly, converting it to another color space,
// performing computations on the color component values, etc."
// So we simply turn "none"s into zeros here. Note that this does not happen for
// interpolations.
void Color::ResolveMissingComponents() {}

void Color::ConvertToColorSpace(ColorSpace destination_color_space,
                                bool resolve_missing_components) {}

SkColor4f Color::toSkColor4f() const {}

SkColor4f
Color::ToGradientStopSkColor4f(ColorSpace interpolation_space) const {}

// static
bool Color::IsBakedGamutMappingEnabled() {}

SkColor4f Color::ToSkColor4fInternal(bool gamut_map_oklab_oklch) const {}

float Color::PremultiplyColor() {}

void Color::UnpremultiplyColor() {}

// This converts -0.0 to 0.0, so that they have the same hash value. This
// ensures that equal FontDescription have the same hash value.
float NormalizeSign(float number) {}

unsigned Color::GetHash() const {}

int Color::Red() const {}
int Color::Green() const {}
int Color::Blue() const {}

RGBA32 Color::Rgb() const {}

bool Color::ParseHexColor(const LChar* name, unsigned length, Color& color) {}

bool Color::ParseHexColor(const UChar* name, unsigned length, Color& color) {}

bool Color::ParseHexColor(const StringView& name, Color& color) {}

int DifferenceSquared(const Color& c1, const Color& c2) {}

bool Color::SetFromString(const String& name) {}

// static
String Color::ColorSpaceToString(Color::ColorSpace color_space) {}

static String ColorParamToString(float param, int precision = 6) {}

String Color::SerializeAsCanvasColor() const {}

String Color::SerializeLegacyColorAsCSSColor() const {}

String Color::SerializeInternal() const {}

String Color::SerializeAsCSSColor() const {}

String Color::NameForLayoutTreeAsText() const {}

bool Color::SetNamedColor(const String& name) {}

Color Color::Light() const {}

Color Color::Dark() const {}

Color Color::Blend(const Color& source) const {}

Color Color::BlendWithWhite() const {}

void Color::GetRGBA(float& r, float& g, float& b, float& a) const {}

void Color::GetRGBA(double& r, double& g, double& b, double& a) const {}

// Hue, max and min are returned in range of 0.0 to 1.0.
void Color::GetHueMaxMin(double& hue, double& max, double& min) const {}

// Hue, saturation and lightness are returned in range of 0.0 to 1.0.
void Color::GetHSL(double& hue, double& saturation, double& lightness) const {}

// Output parameters hue, white and black are in the range 0.0 to 1.0.
void Color::GetHWB(double& hue, double& white, double& black) const {}

Color ColorFromPremultipliedARGB(RGBA32 pixel_color) {}

RGBA32 PremultipliedARGBFromColor(const Color& color) {}

// From https://www.w3.org/TR/css-color-4/#interpolation
// If the host syntax does not define what color space interpolation should
// take place in, it defaults to Oklab.
// However, user agents may handle interpolation between legacy sRGB color
// formats (hex colors, named colors, rgb(), hsl() or hwb() and the equivalent
// alpha-including forms) in gamma-encoded sRGB space.
Color::ColorSpace Color::GetColorInterpolationSpace() const {}

// static
String Color::SerializeInterpolationSpace(
    Color::ColorSpace color_space,
    Color::HueInterpolationMethod hue_interpolation_method) {}

static float ResolveNonFiniteChannel(float value,
                                     float negative_infinity_substitution,
                                     float positive_infinity_substitution) {}

void Color::ResolveNonFiniteValues() {}

std::ostream& operator<<(std::ostream& os, const Color& color) {}

}  // namespace blink