This source file includes following definitions.
- getLinearRgbLUT
- getDeviceRgbLUT
- getConversionLUT
- convertColor
#include "config.h"
#include "platform/graphics/ColorSpace.h"
#include "wtf/MathExtras.h"
namespace WebCore {
namespace ColorSpaceUtilities {
static const uint8_t* getLinearRgbLUT()
{
static uint8_t linearRgbLUT[256];
static bool initialized;
if (!initialized) {
for (unsigned i = 0; i < 256; i++) {
float color = i / 255.0f;
color = (color <= 0.04045f ? color / 12.92f : pow((color + 0.055f) / 1.055f, 2.4f));
color = std::max(0.0f, color);
color = std::min(1.0f, color);
linearRgbLUT[i] = static_cast<uint8_t>(round(color * 255));
}
initialized = true;
}
return linearRgbLUT;
}
static const uint8_t* getDeviceRgbLUT()
{
static uint8_t deviceRgbLUT[256];
static bool initialized;
if (!initialized) {
for (unsigned i = 0; i < 256; i++) {
float color = i / 255.0f;
color = (powf(color, 1.0f / 2.4f) * 1.055f) - 0.055f;
color = std::max(0.0f, color);
color = std::min(1.0f, color);
deviceRgbLUT[i] = static_cast<uint8_t>(round(color * 255));
}
initialized = true;
}
return deviceRgbLUT;
}
const uint8_t* getConversionLUT(ColorSpace dstColorSpace, ColorSpace srcColorSpace)
{
if (srcColorSpace == dstColorSpace)
return 0;
if ((srcColorSpace != ColorSpaceLinearRGB && srcColorSpace != ColorSpaceDeviceRGB)
|| (dstColorSpace != ColorSpaceLinearRGB && dstColorSpace != ColorSpaceDeviceRGB))
return 0;
if (dstColorSpace == ColorSpaceLinearRGB)
return getLinearRgbLUT();
if (dstColorSpace == ColorSpaceDeviceRGB)
return getDeviceRgbLUT();
ASSERT_NOT_REACHED();
return 0;
}
Color convertColor(const Color& srcColor, ColorSpace dstColorSpace, ColorSpace srcColorSpace)
{
const uint8_t* lookupTable = getConversionLUT(dstColorSpace, srcColorSpace);
if (!lookupTable)
return srcColor;
return Color(lookupTable[srcColor.red()], lookupTable[srcColor.green()], lookupTable[srcColor.blue()], srcColor.alpha());
}
}
}