Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
Loading...
Searching...
No Matches
color_space_tests.cpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#include "color_space.hpp"
8
9using namespace rendering_engine;
10
11TEST(ColorSpace, SRGBEndpointsAndPiecewiseBoundary)
12{
13 // Reference values evaluated independently in double precision.
14 const float inputs[] = {0, 1, 0.04f, 0.04045f, 0.041f, 0.5f};
15 const float expected[] = {0, 1, 0.0030959752f, 0.0031308050f, 0.0031742787f, 0.21404114f};
16 for (unsigned i = 0; i < 6; ++i)
17 EXPECT_NEAR(ColorSpace::SRGBToLinear(inputs[i]), expected[i], 1e-7f) << inputs[i];
18}
19
20TEST(ColorSpace, LinearEndpointsAndPiecewiseBoundary)
21{
22 const float inputs[] = {0, 1, 0.003f, 0.0031308f, 0.0032f, 0.5f};
23 const float expected[] = {0, 1, 0.03876f, 0.040449936f, 0.041323359f, 0.73535698f};
24 for (unsigned i = 0; i < 6; ++i)
25 EXPECT_NEAR(ColorSpace::LinearToSRGB(inputs[i]), expected[i], 2e-7f) << inputs[i];
26}
27
28TEST(ColorSpace, RepresentativeValuesRoundTripInBothDirections)
29{
30 for (float value : {0.0f, 0.1f, 0.25f, 0.5f, 0.75f, 1.0f})
31 {
32 EXPECT_NEAR(ColorSpace::LinearToSRGB(ColorSpace::SRGBToLinear(value)), value, 2e-6f);
33 EXPECT_NEAR(ColorSpace::SRGBToLinear(ColorSpace::LinearToSRGB(value)), value, 2e-6f);
34 }
35}
36
37TEST(ColorSpace, VectorOverloadsConvertEachChannelIndependently)
38{
39 const glm::vec3 srgb(0.02f, 0.5f, 1.0f), linear(0.001f, 0.25f, 0.75f);
42 ColorSpace::SRGBToLinear(srgb.z)), 1e-7f);
45 ColorSpace::LinearToSRGB(linear.z)), 1e-7f);
46}
Color space conversion utilities (sRGB <-> linear).
static float LinearToSRGB(float c)
Converts a single linear channel to sRGB space.
static float SRGBToLinear(float c)
Converts a single sRGB channel to linear space.
TEST(ColorSpace, SRGBEndpointsAndPiecewiseBoundary)
void ExpectNear(const glm::vec< N, T, Q > &actual, const glm::vec< N, T, Q > &expected, float tolerance=1e-5f)