From ee6eaedc5b3191df331060e730dff8a35f608b4e Mon Sep 17 00:00:00 2001 From: wkktoria <127852624+wkktoria@users.noreply.github.com> Date: Fri, 26 Jan 2024 12:28:41 +0100 Subject: [PATCH 1/3] Add test for generating lab color --- include/faker-cxx/Color.h | 10 ++++++++++ src/modules/color/Color.cpp | 5 +++++ src/modules/color/ColorTest.cpp | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/include/faker-cxx/Color.h b/include/faker-cxx/Color.h index 6b6db8b79..1d91d1e12 100644 --- a/include/faker-cxx/Color.h +++ b/include/faker-cxx/Color.h @@ -84,5 +84,15 @@ class Color * @endcode */ static std::string cmyk(); + + /** + * @brief Return a LAB color + * + * @returns LAB color formatted with lab(X,X,X) + * @code + * Color::lab() // "lab(98, 2, -2)" + * @endcode + */ + static std::string lab(); }; } diff --git a/src/modules/color/Color.cpp b/src/modules/color/Color.cpp index d4834844e..8f86a9db9 100644 --- a/src/modules/color/Color.cpp +++ b/src/modules/color/Color.cpp @@ -102,4 +102,9 @@ std::string Color::cmyk() return FormatHelper::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key); } +std::string Color::lab() +{ + return ""; +} + } diff --git a/src/modules/color/ColorTest.cpp b/src/modules/color/ColorTest.cpp index 8d866f485..cfeaaad41 100644 --- a/src/modules/color/ColorTest.cpp +++ b/src/modules/color/ColorTest.cpp @@ -192,3 +192,21 @@ TEST_F(ColorTest, shouldGenerateCmykColor) ASSERT_TRUE(0. <= yellow && yellow <= 1.); ASSERT_TRUE(0. <= key && key <= 1.); } + +TEST_F(ColorTest, shouldGenerateLabColor) +{ + const auto generatedLabColor = faker::Color::lab(); + const auto labValues = faker::StringHelper::split(generatedLabColor.substr(4, generatedLabColor.size() - 1), " "); + + int lightness, redGreenValue, blueYellowValue; + + std::from_chars(labValues[0].data(), labValues[0].data() + labValues[0].size(), lightness); + std::from_chars(labValues[1].data(), labValues[1].data() + labValues[1].size(), redGreenValue); + std::from_chars(labValues[2].data(), labValues[2].data() + labValues[2].size(), blueYellowValue); + + ASSERT_TRUE(generatedLabColor.starts_with("lab(")); + ASSERT_TRUE(generatedLabColor.ends_with(")")); + ASSERT_TRUE(lightness >= 0 && lightness <= 100); + ASSERT_TRUE(redGreenValue >= -128 && redGreenValue <= 128); + ASSERT_TRUE(blueYellowValue >= -128 && blueYellowValue <= 128); +} From 9b983bada3d6a0f035dfb7d0b377364ad8bb7011 Mon Sep 17 00:00:00 2001 From: wkktoria <127852624+wkktoria@users.noreply.github.com> Date: Fri, 26 Jan 2024 12:38:38 +0100 Subject: [PATCH 2/3] Implement lab function --- src/modules/color/Color.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/modules/color/Color.cpp b/src/modules/color/Color.cpp index 8f86a9db9..0da816a05 100644 --- a/src/modules/color/Color.cpp +++ b/src/modules/color/Color.cpp @@ -104,7 +104,11 @@ std::string Color::cmyk() std::string Color::lab() { - return ""; + const std::integral auto lightness = Number::integer(100); + const std::integral auto redGreenValue = Number::integer(-128, 128); + const std::integral auto blueYellowValue = Number::integer(-128, 128); + + return FormatHelper::format("lab({}, {}, {})", lightness, redGreenValue, blueYellowValue); } } From 07fe4e661033f8bd3502a5bc10e938e18b238be2 Mon Sep 17 00:00:00 2001 From: wkktoria <127852624+wkktoria@users.noreply.github.com> Date: Fri, 26 Jan 2024 12:52:07 +0100 Subject: [PATCH 3/3] Use floating-point numbers in lab function --- include/faker-cxx/Color.h | 2 +- src/modules/color/Color.cpp | 8 ++++---- src/modules/color/ColorTest.cpp | 17 +++++++++-------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/faker-cxx/Color.h b/include/faker-cxx/Color.h index 1d91d1e12..12bdf4958 100644 --- a/include/faker-cxx/Color.h +++ b/include/faker-cxx/Color.h @@ -90,7 +90,7 @@ class Color * * @returns LAB color formatted with lab(X,X,X) * @code - * Color::lab() // "lab(98, 2, -2)" + * Color::lab() // "lab(98.74, 2.18, -2.35)" * @endcode */ static std::string lab(); diff --git a/src/modules/color/Color.cpp b/src/modules/color/Color.cpp index 0da816a05..a0a0fa893 100644 --- a/src/modules/color/Color.cpp +++ b/src/modules/color/Color.cpp @@ -104,11 +104,11 @@ std::string Color::cmyk() std::string Color::lab() { - const std::integral auto lightness = Number::integer(100); - const std::integral auto redGreenValue = Number::integer(-128, 128); - const std::integral auto blueYellowValue = Number::integer(-128, 128); + const std::floating_point auto lightness = Number::decimal(100.0); + const std::floating_point auto redGreenValue = Number::decimal(-128.0, 128.0); + const std::floating_point auto blueYellowValue = Number::decimal(-128.0, 128.0); - return FormatHelper::format("lab({}, {}, {})", lightness, redGreenValue, blueYellowValue); + return FormatHelper::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, redGreenValue, blueYellowValue); } } diff --git a/src/modules/color/ColorTest.cpp b/src/modules/color/ColorTest.cpp index cfeaaad41..acb37c6eb 100644 --- a/src/modules/color/ColorTest.cpp +++ b/src/modules/color/ColorTest.cpp @@ -198,15 +198,16 @@ TEST_F(ColorTest, shouldGenerateLabColor) const auto generatedLabColor = faker::Color::lab(); const auto labValues = faker::StringHelper::split(generatedLabColor.substr(4, generatedLabColor.size() - 1), " "); - int lightness, redGreenValue, blueYellowValue; - - std::from_chars(labValues[0].data(), labValues[0].data() + labValues[0].size(), lightness); - std::from_chars(labValues[1].data(), labValues[1].data() + labValues[1].size(), redGreenValue); - std::from_chars(labValues[2].data(), labValues[2].data() + labValues[2].size(), blueYellowValue); + auto offset = labValues[0].size(); + const auto lightness = std::stod(labValues[0].data(), &offset); + offset = labValues[1].size(); + const auto redGreenValue = std::stod(labValues[1].data(), &offset); + offset = labValues[2].size(); + const auto blueYellowValue = std::stod(labValues[2].data(), &offset); ASSERT_TRUE(generatedLabColor.starts_with("lab(")); ASSERT_TRUE(generatedLabColor.ends_with(")")); - ASSERT_TRUE(lightness >= 0 && lightness <= 100); - ASSERT_TRUE(redGreenValue >= -128 && redGreenValue <= 128); - ASSERT_TRUE(blueYellowValue >= -128 && blueYellowValue <= 128); + ASSERT_TRUE(lightness >= 0. && lightness <= 100.); + ASSERT_TRUE(redGreenValue >= -128. && redGreenValue <= 128.); + ASSERT_TRUE(blueYellowValue >= -128. && blueYellowValue <= 128.); }