#pragma once
#include "ColorTypes.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <math.h>
namespace WebCore {
template<typename> struct SRGBA;
float lightness(const SRGBA<float>&);
float luminance(const SRGBA<float>&);
float contrastRatio(const SRGBA<float>&, const SRGBA<float>&);
SRGBA<float> premultiplied(const SRGBA<float>&);
SRGBA<float> unpremultiplied(const SRGBA<float>&);
SRGBA<uint8_t> premultipliedFlooring(SRGBA<uint8_t>);
SRGBA<uint8_t> premultipliedCeiling(SRGBA<uint8_t>);
SRGBA<uint8_t> unpremultiplied(SRGBA<uint8_t>);
uint8_t convertPrescaledToComponentByte(float);
uint8_t convertToComponentByte(float);
constexpr float convertToComponentFloat(uint8_t);
template<typename ComponentType> constexpr uint8_t clampToComponentByte(ComponentType);
template<typename ComponentType> constexpr float clampToComponentFloat(ComponentType);
template<typename T> T convertComponentByteTo(uint8_t);
template<typename T> T convertComponentFloatTo(float);
template<template<typename> typename ColorType> ColorType<uint8_t> convertToComponentBytes(const ColorType<float>&);
template<template<typename> typename ColorType> constexpr ColorType<float> convertToComponentFloats(const ColorType<uint8_t>&);
template<template<typename> typename ColorType, typename... ComponentType> constexpr ColorType<uint8_t> clampToComponentBytes(ComponentType...);
template<template<typename> typename ColorType, typename... ComponentType> constexpr ColorType<float> clampToComponentFloats(ComponentType...);
template<typename ColorType, typename Functor> ColorType colorByModifingEachNonAlphaComponent(const ColorType&, Functor&&);
template<typename ColorType> constexpr ColorType colorWithOverridenAlpha(const ColorType&, uint8_t overrideAlpha);
template<typename ColorType> ColorType colorWithOverridenAlpha(const ColorType&, float overrideAlpha);
template<typename ColorType> constexpr ColorType invertedColorWithOverridenAlpha(const ColorType&, uint8_t overrideAlpha);
template<typename ColorType> ColorType invertedColorWithOverridenAlpha(const ColorType&, float overrideAlpha);
template<typename ColorType> constexpr bool isBlack(const ColorType&);
template<typename ColorType> constexpr bool isWhite(const ColorType&);
constexpr uint16_t fastMultiplyBy255(uint16_t);
constexpr uint16_t fastDivideBy255(uint16_t);
inline uint8_t convertPrescaledToComponentByte(float f)
{
return std::clamp(std::lround(f), 0l, 255l);
}
inline uint8_t convertToComponentByte(float f)
{
return std::clamp(std::lround(f * 255.0f), 0l, 255l);
}
constexpr float convertToComponentFloat(uint8_t byte)
{
return byte / 255.0f;
}
template<typename ComponentType> constexpr uint8_t clampToComponentByte(ComponentType component)
{
return std::clamp<ComponentType>(component, 0, 255);
}
template<typename ComponentType> constexpr float clampToComponentFloat(ComponentType component)
{
return std::clamp<ComponentType>(component, 0, 1);
}
template<> constexpr uint8_t convertComponentByteTo<uint8_t>(uint8_t value)
{
return value;
}
template<> constexpr float convertComponentByteTo<float>(uint8_t value)
{
return convertToComponentFloat(value);
}
template<> inline uint8_t convertComponentFloatTo<uint8_t>(float value)
{
return convertToComponentByte(value);
}
template<> inline float convertComponentFloatTo<float>(float value)
{
return clampToComponentFloat(value);
}
template<template<typename> typename ColorType> inline ColorType<uint8_t> convertToComponentBytes(const ColorType<float>& color)
{
auto components = asColorComponents(color);
return { convertToComponentByte(components[0]), convertToComponentByte(components[1]), convertToComponentByte(components[2]), convertToComponentByte(components[3]) };
}
template<template<typename> typename ColorType> constexpr ColorType<float> convertToComponentFloats(const ColorType<uint8_t>& color)
{
auto components = asColorComponents(color);
return { convertToComponentFloat(components[0]), convertToComponentFloat(components[1]), convertToComponentFloat(components[2]), convertToComponentFloat(components[3]) };
}
template<template<typename> typename ColorType, typename... ComponentType> constexpr ColorType<uint8_t> clampToComponentBytes(ComponentType... components)
{
return { clampToComponentByte(components)... };
}
template<template<typename> typename ColorType, typename... ComponentType> constexpr ColorType<float> clampToComponentFloats(ComponentType... components)
{
return { clampToComponentFloat(components)... };
}
template<typename ColorType, typename Functor> ColorType colorByModifingEachNonAlphaComponent(const ColorType& color, Functor&& functor)
{
auto copy = color;
copy.red = std::invoke(functor, color.red);
copy.green = std::invoke(functor, color.green);
copy.blue = std::invoke(std::forward<Functor>(functor), color.blue);
return copy;
}
template<typename ColorType> constexpr ColorType colorWithOverridenAlpha(const ColorType& color, uint8_t overrideAlpha)
{
auto copy = color;
copy.alpha = convertComponentByteTo<decltype(copy.alpha)>(overrideAlpha);
return copy;
}
template<typename ColorType> ColorType colorWithOverridenAlpha(const ColorType& color, float overrideAlpha)
{
auto copy = color;
copy.alpha = convertComponentFloatTo<decltype(copy.alpha)>(overrideAlpha);
return copy;
}
template<typename ColorType> constexpr ColorType invertedColorWithOverridenAlpha(const ColorType& color, uint8_t overrideAlpha)
{
auto copy = colorByModifingEachNonAlphaComponent(color, [] (auto component) {
return ComponentTraits<decltype(component)>::maxValue - component;
});
copy.alpha = convertComponentByteTo<decltype(copy.alpha)>(overrideAlpha);
return copy;
}
template<typename ColorType> ColorType invertedColorWithOverridenAlpha(const ColorType& color, float overrideAlpha)
{
auto copy = colorByModifingEachNonAlphaComponent(color, [] (auto component) {
return ComponentTraits<decltype(component)>::maxValue - component;
});
copy.alpha = convertComponentFloatTo<decltype(copy.alpha)>(overrideAlpha);
return copy;
}
template<typename ColorType> constexpr bool isBlack(const ColorType& color)
{
constexpr auto min = ComponentTraits<typename ColorType::ComponentType>::minValue;
constexpr auto max = ComponentTraits<typename ColorType::ComponentType>::maxValue;
auto [c1, c2, c3, alpha] = color;
return c1 == min && c2 == min && c3 == min && alpha == max;
}
template<typename ColorType> constexpr bool isWhite(const ColorType& color)
{
constexpr auto max = ComponentTraits<typename ColorType::ComponentType>::maxValue;
auto [c1, c2, c3, alpha] = color;
return c1 == max && c2 == max && c3 == max && alpha == max;
}
constexpr uint16_t fastMultiplyBy255(uint16_t value)
{
return (value << 8) - value;
}
constexpr uint16_t fastDivideBy255(uint16_t value)
{
uint16_t approximation = value >> 8;
uint16_t remainder = value - (approximation * 255) + 1;
return approximation + (remainder >> 8);
}
}