diff --git a/include/faker-cxx/Color.h b/include/faker-cxx/Color.h index 12bdf4958..0d6bb76c1 100644 --- a/include/faker-cxx/Color.h +++ b/include/faker-cxx/Color.h @@ -94,5 +94,25 @@ class Color * @endcode */ static std::string lab(); + + /** + * @brief Return a HSB color + * + * @returns HSB color formatted with hsb(X,X,X) + * @code + * Color::hsb() // "hsb(37, 82, 50)" + * @endcode + */ + static std::string hsb(); + + /** + * @brief Return a HSV color + * + * @returns HSV color formatted with hsv(X,X,X) + * @code + * Color::hsv() // "hsv(21, 67, 39)" + * @endcode + */ + static std::string hsv(); }; } diff --git a/src/modules/color/Color.cpp b/src/modules/color/Color.cpp index a0a0fa893..eeed64736 100644 --- a/src/modules/color/Color.cpp +++ b/src/modules/color/Color.cpp @@ -111,4 +111,22 @@ std::string Color::lab() return FormatHelper::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, redGreenValue, blueYellowValue); } +std::string Color::hsb() +{ + const std::integral auto hue = Number::integer(360); + const std::integral auto saturation = Number::integer(100); + const std::integral auto brightness = Number::integer(100); + + return FormatHelper::format("hsb({}, {}, {})", hue, saturation, brightness); +} + +std::string Color::hsv() +{ + const std::integral auto hue = Number::integer(360); + const std::integral auto saturation = Number::integer(100); + const std::integral auto value = Number::integer(100); + + return FormatHelper::format("hsv({}, {}, {})", hue, saturation, value); +} + } diff --git a/src/modules/color/ColorTest.cpp b/src/modules/color/ColorTest.cpp index acb37c6eb..6347e89e6 100644 --- a/src/modules/color/ColorTest.cpp +++ b/src/modules/color/ColorTest.cpp @@ -211,3 +211,39 @@ TEST_F(ColorTest, shouldGenerateLabColor) ASSERT_TRUE(redGreenValue >= -128. && redGreenValue <= 128.); ASSERT_TRUE(blueYellowValue >= -128. && blueYellowValue <= 128.); } + +TEST_F(ColorTest, shouldGenerateHsb) +{ + const auto generatedHsbColor = faker::Color::hsb(); + const auto hsbValues = faker::StringHelper::split(generatedHsbColor.substr(4, generatedHsbColor.size() - 1), " "); + + int hue, staturation, brightness; + + std::from_chars(hsbValues[0].data(), hsbValues[0].data() + hsbValues[0].size(), hue); + std::from_chars(hsbValues[1].data(), hsbValues[1].data() + hsbValues[1].size(), staturation); + std::from_chars(hsbValues[2].data(), hsbValues[2].data() + hsbValues[2].size(), brightness); + + ASSERT_TRUE(generatedHsbColor.starts_with("hsb(")); + ASSERT_TRUE(generatedHsbColor.ends_with(")")); + ASSERT_TRUE(hue >= 0 && hue <= 360); + ASSERT_TRUE(staturation >= 0 && staturation <= 100); + ASSERT_TRUE(brightness >= 0 && brightness <= 100); +} + +TEST_F(ColorTest, shouldGenerateHsv) +{ + const auto generatedHsvColor = faker::Color::hsv(); + const auto hsvValues = faker::StringHelper::split(generatedHsvColor.substr(4, generatedHsvColor.size() - 1), " "); + + int hue, staturation, brightness; + + std::from_chars(hsvValues[0].data(), hsvValues[0].data() + hsvValues[0].size(), hue); + std::from_chars(hsvValues[1].data(), hsvValues[1].data() + hsvValues[1].size(), staturation); + std::from_chars(hsvValues[2].data(), hsvValues[2].data() + hsvValues[2].size(), brightness); + + ASSERT_TRUE(generatedHsvColor.starts_with("hsv(")); + ASSERT_TRUE(generatedHsvColor.ends_with(")")); + ASSERT_TRUE(hue >= 0 && hue <= 360); + ASSERT_TRUE(staturation >= 0 && staturation <= 100); + ASSERT_TRUE(brightness >= 0 && brightness <= 100); +} \ No newline at end of file