Skip to content

Commit

Permalink
add hsl-function to color module
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeshkhadka committed Oct 10, 2023
1 parent 51ba70d commit a19f739
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/faker-cxx/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
22 changes: 22 additions & 0 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(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);
}

}
39 changes: 39 additions & 0 deletions src/modules/color/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit a19f739

Please sign in to comment.