Skip to content

Commit

Permalink
Add lch function to Color module (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
saharmyhre authored Nov 2, 2023
1 parent 033637a commit f152795
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 @@ -62,5 +62,17 @@ class Color
* @endcode
*/
static std::string hsl(bool includeAlpha = false);

/**
* @brief Returns an LCH color.
*
* @param includeAlpha Adds an alpha value to the color (LCHA). Defaults to `false`.
* @returns LCH color formatted with lch(X,X,X) or lcha(X,X,X,X).
* @code
* Color::lch() // "lch(0, 20, 100)"
* Color::lch(true) // "lcha(0, 0, 100, 0.50)"
* @endcode
*/
static std::string lch(bool includeAlpha = false);
};
}
21 changes: 21 additions & 0 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,26 @@ std::string Color::hsl(bool includeAlpha)

return fmt::format("hsla({}, {}, {}, {})", hue, saturation, lightness, formattedAlpha);
}
std::string Color::lch(bool includeAlpha)
{
const std::integral auto luminance = Number::integer(100);
const std::integral auto chroma = Number::integer(100);
const std::integral auto hue = Number::integer(360);

if (!includeAlpha)
{
return fmt::format("lch({}, {}, {})", luminance, chroma, hue);
}

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("lcha({}, {}, {}, {})", luminance, chroma, hue, formattedAlpha);
}

}
40 changes: 40 additions & 0 deletions src/modules/color/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,43 @@ TEST_F(ColorTest, shouldGenerateHslWithAlpha)
ASSERT_TRUE(lightness >= 0 && lightness <= 100);
ASSERT_TRUE(alpha >= 0 && alpha <= 1);
}

TEST_F(ColorTest, shouldGenerateLchWithoutAlpha)
{
const auto generatedLchColor = faker::Color::lch();
const auto lchValues = faker::StringHelper::split(generatedLchColor.substr(4, generatedLchColor.size() - 1), " ");

int luminance, chroma, hue;

std::from_chars(lchValues[0].data(), lchValues[0].data() + lchValues[0].size(), luminance);
std::from_chars(lchValues[1].data(), lchValues[1].data() + lchValues[1].size(), chroma);
std::from_chars(lchValues[2].data(), lchValues[2].data() + lchValues[2].size(), hue);

ASSERT_TRUE(generatedLchColor.starts_with("lch("));
ASSERT_TRUE(generatedLchColor.ends_with(")"));
ASSERT_TRUE(luminance >= 0 && luminance <= 100);
ASSERT_TRUE(chroma >= 0 && chroma <= 100);
ASSERT_TRUE(hue >= 0 && hue <= 360);
}

TEST_F(ColorTest, shouldGenerateLchWithAlpha)
{
const auto generatedLchaColor = faker::Color::lch(true);
const auto lchValues = faker::StringHelper::split(generatedLchaColor.substr(5, generatedLchaColor.size() - 1), " ");

int luminance, chroma, hue;

std::from_chars(lchValues[0].data(), lchValues[0].data() + lchValues[0].size(), luminance);
std::from_chars(lchValues[1].data(), lchValues[1].data() + lchValues[1].size(), chroma);
std::from_chars(lchValues[2].data(), lchValues[2].data() + lchValues[2].size(), hue);

auto offset = lchValues[3].size();
const auto alpha = std::stod(lchValues[3].data(), &offset);

ASSERT_TRUE(generatedLchaColor.starts_with("lcha("));
ASSERT_TRUE(generatedLchaColor.ends_with(")"));
ASSERT_TRUE(luminance >= 0 && luminance <= 100);
ASSERT_TRUE(chroma >= 0 && chroma <= 100);
ASSERT_TRUE(hue >= 0 && hue <= 360);
ASSERT_TRUE(alpha >= 0 && alpha <= 1);
}

0 comments on commit f152795

Please sign in to comment.