Red and green have the same RGB average. Why do they look different?
Convert five sRGB swatches to CIELAB with Little CMS and trace the tone curves, color matrix, and L* calculation.
Example result In D50 CIELAB, Little CMS 2.20 gives saturated red L* = 54.3 and saturated green L* = 87.8. Both input swatches have an arithmetic RGB average of 85.
How does a color photo become a grayscale preview?
An engraving editor needs a rule for turning color into shades of gray. Pure red (255,0,0) and pure green (0,255,0) both average to 85 in sRGB, so a direct channel average displays them as the same gray. Little CMS, an ICC color-management library started by Marti Maria, can transform them to CIELAB using its built-in sRGB and Lab profiles. This experiment compares their L* lightness. Little CMS project home
An engraving editor can interpret each pixel through the input ICC profile before using L* as the input to a grayscale preview curve. Dithering, layer inversion, and tone mapping have separate settings, and material response calls for separate calibration.
From tone curves and a matrix to L*
cmsCreate_sRGBProfile creates a virtual input profile with D65 white, primary chromaticities, and the sRGB tone curve. Once code values are normalized to 0–1, the curve has a linear section at and below 0.04045 and a power section with exponent 2.4 above it. BuildRGBInputMatrixShaper reads the three tone-curve tags and appends a matrix that maps linear RGB to profile connection space (PCS) XYZ.
The target, cmsCreateLab4Profile(nullptr), uses D50 reference white. When the pipeline connects XYZ to Lab, it inserts a conversion stage. cmsXYZ2Lab computes L* as 116f(Y/Yₙ) − 16, where Yₙ is the Y value of the reference white. The function f is linear below (6/29)³ and uses the cube root above it. The example takes TYPE_RGB_8 and returns TYPE_Lab_DBL, with lightness in the first double.
The source profile defines what the RGB numbers mean. These generated swatches are interpreted as sRGB. Artwork tagged with another color profile calls for that source profile. L* estimates perceptual lightness relative to a reference white. Raster values still depend on the tone curve chosen by the editor.
Built-in sRGB profile and tone curve · Tone curves and matrix stages · Connecting XYZ and Lab · Piecewise XYZ-to-Lab calculation
Build and run locally
Use a terminal on macOS or Linux with curl, tar, make, and a C++17 toolchain. The example statically links Little CMS built from the 2.20 source and needs no external photo or ICC file. Tested on Apple Silicon macOS.
Download the C++17 example · Plain-text build commands
Read the example code
#include <array>
#include <iomanip>
#include <iostream>
#include <memory>
#include <lcms2.h>
int main() {
const std::unique_ptr<void, decltype(&cmsCloseProfile)> srgb(
cmsCreate_sRGBProfile(), cmsCloseProfile);
const std::unique_ptr<void, decltype(&cmsCloseProfile)> lab(
cmsCreateLab4Profile(nullptr), cmsCloseProfile);
if (!srgb || !lab) {
std::cerr << "Could not create profiles\n";
return 1;
}
const std::unique_ptr<void, decltype(&cmsDeleteTransform)> transform(
cmsCreateTransform(srgb.get(), TYPE_RGB_8, lab.get(), TYPE_Lab_DBL,
INTENT_RELATIVE_COLORIMETRIC, cmsFLAGS_NOOPTIMIZE),
cmsDeleteTransform);
if (!transform) {
std::cerr << "Could not create transform\n";
return 1;
}
struct Swatch {
const char* name;
std::array<cmsUInt8Number, 3> rgb;
};
const std::array<Swatch, 5> swatches{{
{"black", {0, 0, 0}},
{"middle gray", {128, 128, 128}},
{"white", {255, 255, 255}},
{"red", {255, 0, 0}},
{"green", {0, 255, 0}}
}};
std::cout << "Little CMS encoded version " << cmsGetEncodedCMMversion() << '\n';
std::cout << std::fixed << std::setprecision(1);
for (const auto& swatch : swatches) {
std::array<double, 3> result{};
cmsDoTransform(transform.get(), swatch.rgb.data(), result.data(), 1);
std::cout << swatch.name << ": L*=" << result[0] << '\n';
}
}mkdir little-cms-study && cd little-cms-study
curl -fsSL https://codeload.github.com/mm2/Little-CMS/tar.gz/refs/tags/lcms2.20 -o little-cms.tgz
tar -xzf little-cms.tgz
curl -fsSLO https://www.juyaoai.com/examples/little-cms-lab-lightness/demo.cpp
(cd Little-CMS-lcms2.20 && ./configure --disable-shared --enable-static --without-tiff --without-jpeg && make -C src -j2)
c++ -std=c++17 -I Little-CMS-lcms2.20/include demo.cpp \
Little-CMS-lcms2.20/src/.libs/liblcms2.a -lm -pthread -o demo
./demoOutput and a follow-up experiment
Little CMS encoded version 2200
black: L*=-0.0
middle gray: L*=53.6
white: L*=100.0
red: L*=54.3
green: L*=87.8Try (0,0,255) and (85,85,85). Their arithmetic RGB average stays at 85. The blue swatch reads L* = 29.6 and the gray reads 36.1. Floating-point arithmetic and one-decimal formatting can display black as −0.0, which reads as zero lightness.