Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lab function to color module #497

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/faker-cxx/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,15 @@ class Color
* @endcode
*/
static std::string cmyk();

/**
* @brief Return a LAB color
*
* @returns LAB color formatted with lab(X,X,X)
* @code
* Color::lab() // "lab(98.74, 2.18, -2.35)"
* @endcode
*/
static std::string lab();
};
}
9 changes: 9 additions & 0 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,13 @@ std::string Color::cmyk()
return FormatHelper::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key);
}

std::string Color::lab()
{
const std::floating_point auto lightness = Number::decimal<double>(100.0);
const std::floating_point auto redGreenValue = Number::decimal<double>(-128.0, 128.0);
const std::floating_point auto blueYellowValue = Number::decimal<double>(-128.0, 128.0);

return FormatHelper::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, redGreenValue, blueYellowValue);
}

}
19 changes: 19 additions & 0 deletions src/modules/color/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,22 @@ TEST_F(ColorTest, shouldGenerateCmykColor)
ASSERT_TRUE(0. <= yellow && yellow <= 1.);
ASSERT_TRUE(0. <= key && key <= 1.);
}

TEST_F(ColorTest, shouldGenerateLabColor)
{
const auto generatedLabColor = faker::Color::lab();
const auto labValues = faker::StringHelper::split(generatedLabColor.substr(4, generatedLabColor.size() - 1), " ");

auto offset = labValues[0].size();
const auto lightness = std::stod(labValues[0].data(), &offset);
offset = labValues[1].size();
const auto redGreenValue = std::stod(labValues[1].data(), &offset);
offset = labValues[2].size();
const auto blueYellowValue = std::stod(labValues[2].data(), &offset);

ASSERT_TRUE(generatedLabColor.starts_with("lab("));
ASSERT_TRUE(generatedLabColor.ends_with(")"));
ASSERT_TRUE(lightness >= 0. && lightness <= 100.);
ASSERT_TRUE(redGreenValue >= -128. && redGreenValue <= 128.);
ASSERT_TRUE(blueYellowValue >= -128. && blueYellowValue <= 128.);
}
Loading