Skip to content

Commit

Permalink
CMYK function added (#223)
Browse files Browse the repository at this point in the history
* CMYK function added to Color module (#187)

* CMYK test added (#187)

* files formatted

* CMYK function implemented (#187)
  • Loading branch information
AltairFromAquila authored Nov 8, 2023
1 parent fc590e5 commit 52ca738
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
12 changes: 11 additions & 1 deletion include/faker-cxx/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ class Color
* Color::lch(true) // "lcha(0, 0, 100, 0.50)"
* @endcode
*/
static std::string lch(bool includeAlpha = false);
static std::string lch(bool includeAlpha = false);

/**
* @brief Return a CMYK color
*
* @returns CMYK color formatted with cmyk(X,X,X,X)
* @code
* Color::cmyk() // "cmyk(0.72, 0.88, 0.00, 0.06)"
* @endcode
*/
static std::string cmyk();
};
}
10 changes: 10 additions & 0 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ std::string Color::lch(bool includeAlpha)
return fmt::format("lcha({}, {}, {}, {})", luminance, chroma, hue, formattedAlpha);
}

std::string Color::cmyk()
{
const std::floating_point auto cyan = Number::decimal<double>(1.);
const std::floating_point auto magenta = Number::decimal<double>(1.);
const std::floating_point auto yellow = Number::decimal<double>(1.);
const std::floating_point auto key = Number::decimal<double>(1.);

return fmt::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key);
}

}
23 changes: 23 additions & 0 deletions src/modules/color/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,26 @@ TEST_F(ColorTest, shouldGenerateLchWithAlpha)
ASSERT_TRUE(hue >= 0 && hue <= 360);
ASSERT_TRUE(alpha >= 0 && alpha <= 1);
}

TEST_F(ColorTest, shouldGenerateCmykColor)
{
const auto generatedCmykColor = faker::Color::cmyk();
const auto cmykValues =
faker::StringHelper::split(generatedCmykColor.substr(5, generatedCmykColor.size() - 1), " ");

auto offset = cmykValues[0].size();
const auto cyan = std::stod(cmykValues[0].data(), &offset);
offset = cmykValues[1].size();
const auto magenta = std::stod(cmykValues[1].data(), &offset);
offset = cmykValues[2].size();
const auto yellow = std::stod(cmykValues[2].data(), &offset);
offset = cmykValues[3].size();
const auto key = std::stod(cmykValues[3].data(), &offset);

ASSERT_TRUE(generatedCmykColor.starts_with("cmyk("));
ASSERT_TRUE(generatedCmykColor.ends_with(")"));
ASSERT_TRUE(0. <= cyan && cyan <= 1.);
ASSERT_TRUE(0. <= magenta && magenta <= 1.);
ASSERT_TRUE(0. <= yellow && yellow <= 1.);
ASSERT_TRUE(0. <= key && key <= 1.);
}

0 comments on commit 52ca738

Please sign in to comment.