From a19f739895b4f44a294b95b4e5a7170ecbc663ae Mon Sep 17 00:00:00 2001 From: sandeshkhadka Date: Tue, 10 Oct 2023 22:08:49 +0545 Subject: [PATCH 1/2] add hsl-function to color module --- include/faker-cxx/Color.h | 12 ++++++++++ src/modules/color/Color.cpp | 22 +++++++++++++++++++ src/modules/color/ColorTest.cpp | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/include/faker-cxx/Color.h b/include/faker-cxx/Color.h index 987d6cfcd..03b2b2386 100644 --- a/include/faker-cxx/Color.h +++ b/include/faker-cxx/Color.h @@ -50,5 +50,17 @@ class Color */ static std::string hex(HexCasing casing = HexCasing::Lower, HexPrefix prefix = HexPrefix::Hash, bool includeAlpha = false); + + /** + * @brief Returns an HSL color. + * + * @param includeAlpha Adds an alpha value to the color (HSLA). Defaults to `false`. + * @returns HSL color formatted with hsl(X,X,X) or hsla(X,X,X,X). + * @code + * Color::hsl() // "hsl(0, 20, 100)" + * Color::hsl(true) // "hsla(0, 0, 100, 0.50)" + * @endcode + */ + static std::string hsl(bool includeAlpha = false); }; } diff --git a/src/modules/color/Color.cpp b/src/modules/color/Color.cpp index 3aa0b0b10..d156d6b59 100644 --- a/src/modules/color/Color.cpp +++ b/src/modules/color/Color.cpp @@ -46,4 +46,26 @@ std::string Color::hex(HexCasing casing, HexPrefix prefix, bool includeAlpha) return String::hexadecimal(6, casing, prefix); } +std::string Color::hsl(bool includeAlpha) +{ + const std::integral auto hue = Number::integer(360); + const std::integral auto saturation = Number::integer(100); + const std::integral auto lightness = Number::integer(100); + + if (!includeAlpha) + { + return fmt::format("hsl({}, {}, {})", hue, saturation, lightness); + } + + const std::floating_point auto alpha = Number::decimal(1); + + std::stringstream ss; + ss << std::fixed; + ss.precision(2); + ss << alpha; + const auto formattedAlpha = ss.str(); + + return fmt::format("hsla({}, {}, {}, {})", hue, saturation, lightness, formattedAlpha); +} + } diff --git a/src/modules/color/ColorTest.cpp b/src/modules/color/ColorTest.cpp index 8682c969c..e4d11a5f8 100644 --- a/src/modules/color/ColorTest.cpp +++ b/src/modules/color/ColorTest.cpp @@ -88,3 +88,42 @@ TEST_F(ColorTest, shouldGenerateHexColorWithAlpha) ASSERT_TRUE(std::ranges::any_of(hexNumber, [hexNumber](char hexNumberCharacter) { return hexUpperCharacters.find(hexNumberCharacter) != std::string::npos; })); } + +TEST_F(ColorTest, shouldGenerateHslWithoutAlpha) +{ + const auto generatedHslColor = faker::Color::hsl(); + const auto hslValues = faker::StringHelper::split(generatedHslColor.substr(4, generatedHslColor.size() - 1), " "); + + int hue, staturation, lightness; + + std::from_chars(hslValues[0].data(), hslValues[0].data() + hslValues[0].size(), hue); + std::from_chars(hslValues[1].data(), hslValues[1].data() + hslValues[1].size(), staturation); + std::from_chars(hslValues[2].data(), hslValues[2].data() + hslValues[2].size(), lightness); + + ASSERT_TRUE(generatedHslColor.starts_with("hsl(")); + ASSERT_TRUE(generatedHslColor.ends_with(")")); + ASSERT_TRUE(hue >= 0 && hue <= 360); + ASSERT_TRUE(staturation >= 0 && staturation <= 100); + ASSERT_TRUE(lightness >= 0 && lightness <= 100); +} + +TEST_F(ColorTest, shouldGenerateHslWithAlpha) +{ + const auto generatedHslaColor = faker::Color::hsl(true); + const auto hslValues = faker::StringHelper::split(generatedHslaColor.substr(5, generatedHslaColor.size() - 1), " "); + + int hue, staturation, lightness; + double alpha; + + std::from_chars(hslValues[0].data(), hslValues[0].data() + hslValues[0].size(), hue); + std::from_chars(hslValues[1].data(), hslValues[1].data() + hslValues[1].size(), staturation); + std::from_chars(hslValues[2].data(), hslValues[2].data() + hslValues[2].size(), lightness); + std::from_chars(hslValues[3].data(), hslValues[3].data() + hslValues[3].size(), alpha); + + ASSERT_TRUE(generatedHslaColor.starts_with("hsla(")); + ASSERT_TRUE(generatedHslaColor.ends_with(")")); + ASSERT_TRUE(hue >= 0 && hue <= 360); + ASSERT_TRUE(staturation >= 0 && staturation <= 100); + ASSERT_TRUE(lightness >= 0 && lightness <= 100); + ASSERT_TRUE(alpha >= 0 && alpha <= 1); +} From 12da92dc55cd8e8363b8133b5b6370a53966fdfb Mon Sep 17 00:00:00 2001 From: sandeshkhadka Date: Tue, 10 Oct 2023 22:46:00 +0545 Subject: [PATCH 2/2] add missing header:iomanip --- src/modules/date/Date.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/modules/date/Date.cpp b/src/modules/date/Date.cpp index 72d96d0d7..abd283286 100644 --- a/src/modules/date/Date.cpp +++ b/src/modules/date/Date.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "data/MonthNames.h" #include "data/WeekdayNames.h" @@ -30,7 +31,8 @@ std::string betweenDate(const auto& from, const auto& to) { if (from > to) { - throw std::runtime_error{fmt::format("Start date is greater than end date. {{from: {}, to: {}}}", serializeTimePoint(from),serializeTimePoint(to))}; + throw std::runtime_error{fmt::format("Start date is greater than end date. {{from: {}, to: {}}}", + serializeTimePoint(from), serializeTimePoint(to))}; } const auto size = std::chrono::duration_cast(to - from).count();