mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibGfx: Correctly round values when computing the luminosity of a Color
Truncating the value is mathematically incorrect, this error made the conversion to grayscale unstable. In other world, calling `to_grayscale` on a gray value would return a different value. As an example, `Color::from_string("#686868ff"sv).to_grayscale()` used to return #676767ff.
This commit is contained in:
parent
bd4f516e64
commit
ff2c6cab55
Notes:
sideshowbarker
2024-07-16 18:26:46 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/ff2c6cab55 Pull-request: https://github.com/SerenityOS/serenity/pull/24315 Reviewed-by: https://github.com/gmta ✅ Reviewed-by: https://github.com/nico ✅
3 changed files with 18 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
|||
set(TEST_SOURCES
|
||||
BenchmarkGfxPainter.cpp
|
||||
BenchmarkJPEGLoader.cpp
|
||||
TestColor.cpp
|
||||
TestDeltaE.cpp
|
||||
TestFontHandling.cpp
|
||||
TestGfxBitmap.cpp
|
||||
|
|
16
Tests/LibGfx/TestColor.cpp
Normal file
16
Tests/LibGfx/TestColor.cpp
Normal file
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Lucas Chollet <lucas.chollet@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGfx/Color.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
TEST_CASE(color)
|
||||
{
|
||||
for (u16 i = 0; i < 256; ++i) {
|
||||
auto const gray = Color(i, i, i);
|
||||
EXPECT_EQ(gray, gray.to_grayscale());
|
||||
}
|
||||
}
|
|
@ -316,7 +316,7 @@ public:
|
|||
|
||||
constexpr u8 luminosity() const
|
||||
{
|
||||
return (red() * 0.2126f + green() * 0.7152f + blue() * 0.0722f);
|
||||
return round_to<u8>(red() * 0.2126f + green() * 0.7152f + blue() * 0.0722f);
|
||||
}
|
||||
|
||||
constexpr float contrast_ratio(Color other)
|
||||
|
|
Loading…
Reference in a new issue