From c932a4aaed1bbf1f0520f0a584247567fa7d37f2 Mon Sep 17 00:00:00 2001 From: Daniel Huang <74921405+daniel7722@users.noreply.github.com> Date: Tue, 8 Oct 2024 22:15:16 +0800 Subject: [PATCH] feat: add hwb function to Color module (#950) --- include/faker-cxx/color.h | 10 ++++++++++ src/modules/color.cpp | 9 +++++++++ tests/modules/color_test.cpp | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/include/faker-cxx/color.h b/include/faker-cxx/color.h index b28842319..2a0c9bf0d 100644 --- a/include/faker-cxx/color.h +++ b/include/faker-cxx/color.h @@ -117,6 +117,16 @@ FAKER_CXX_EXPORT std::string hsb(); */ FAKER_CXX_EXPORT std::string hsv(); +/** + * @brief Return a HWB color + * + * @returns HWB color formatted with hwb(X,X,X) + * @code + * faker::color::hwb() // "hwb(34, 67, 90)" + * @endcode + */ +FAKER_CXX_EXPORT std::string hwb(); + /** * @brief Return a YUV color * diff --git a/src/modules/color.cpp b/src/modules/color.cpp index 14d8834b0..771550359 100644 --- a/src/modules/color.cpp +++ b/src/modules/color.cpp @@ -124,6 +124,15 @@ std::string hsv() return common::format("hsv({}, {}, {})", hue, saturation, value); } +std::string hwb() +{ + std::integral auto hue = number::integer(360); + std::integral auto whiteness = number::integer(100); + std::integral auto blackness = number::integer(100); + + return common::format("hwb({}, {}, {})", hue, whiteness, blackness); +} + std::string yuv() { std::integral auto luminance = number::integer(255); diff --git a/tests/modules/color_test.cpp b/tests/modules/color_test.cpp index c454ceb1c..b2d6ce2d4 100644 --- a/tests/modules/color_test.cpp +++ b/tests/modules/color_test.cpp @@ -274,6 +274,24 @@ TEST_F(ColorTest, shouldGenerateHsv) ASSERT_TRUE(brightness >= 0 && brightness <= 100); } +TEST_F(ColorTest, shouldGenerateHwb) +{ + const auto generatedHwbColor = hwb(); + const auto hwbValues = common::split(generatedHwbColor.substr(4, generatedHwbColor.size() - 1), " "); + + int hue, whiteness, blackness; + + std::from_chars(hwbValues[0].data(), hwbValues[0].data() + hwbValues[0].size(), hue); + std::from_chars(hwbValues[1].data(), hwbValues[1].data() + hwbValues[1].size(), whiteness); + std::from_chars(hwbValues[2].data(), hwbValues[2].data() + hwbValues[2].size(), blackness); + + ASSERT_TRUE(generatedHwbColor.starts_with("hwb(")); + ASSERT_TRUE(generatedHwbColor.ends_with(")")); + ASSERT_TRUE(hue >= 0 && hue <= 360); + ASSERT_TRUE(whiteness >= 0 && whiteness <= 100); + ASSERT_TRUE(blackness >= 0 && blackness <= 100); +} + TEST_F(ColorTest, shouldGenerateYuv) { const auto generatedYuvColor = yuv();