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

refactor color module data #593

Merged
merged 1 commit into from
May 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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(FAKER_SOURCES
src/modules/book/Book.cpp
src/modules/book/BookData.cpp
src/modules/color/Color.cpp
src/modules/color/ColorData.cpp
src/modules/commerce/Commerce.cpp
src/modules/company/Company.cpp
src/modules/computer/Computer.cpp
Expand Down
2 changes: 1 addition & 1 deletion include/faker-cxx/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Color
* Color::name() // "Blue"
* @endcode
*/
static std::string name();
static std::string_view name();

/**
* @brief Returns an RGB color.
Expand Down
102 changes: 38 additions & 64 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#include "faker-cxx/Color.h"

#include <sstream>

#include "../../common/FormatHelper.h"
#include "data/Colors.h"
#include "ColorData.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "faker-cxx/String.h"

namespace faker
{
std::string Color::name()
std::string_view Color::name()
{
return Helper::arrayElement<std::string>(colors);
return Helper::arrayElement(colors);
}

std::string Color::rgb(bool includeAlpha)
Expand All @@ -26,116 +24,92 @@ std::string Color::rgb(bool includeAlpha)
return FormatHelper::format("rgb({}, {}, {})", red, green, blue);
}

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();
std::floating_point auto alpha = Number::decimal(1.0);

return FormatHelper::format("rgba({}, {}, {}, {})", red, green, blue, formattedAlpha);
return FormatHelper::format("rgba({}, {}, {}, {:.2f})", red, green, blue, alpha);
}

std::string Color::hex(HexCasing casing, HexPrefix prefix, bool includeAlpha)
{
if (includeAlpha)
{
return String::hexadecimal(8, casing, prefix);
}

return String::hexadecimal(6, casing, prefix);
return String::hexadecimal(includeAlpha ? 8 : 6, casing, prefix);
}

std::string Color::hsl(bool includeAlpha)
std::string Color::hsl(bool include_alpha)
{
const std::integral auto hue = Number::integer(360);
const std::integral auto saturation = Number::integer(100);
const std::integral auto lightness = Number::integer(100);
std::integral auto hue = Number::integer(360);
std::integral auto saturation = Number::integer(100);
std::integral auto lightness = Number::integer(100);

if (!includeAlpha)
if (!include_alpha)
{
return FormatHelper::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();
std::floating_point auto alpha = Number::decimal(1.0);

return FormatHelper::format("hsla({}, {}, {}, {})", hue, saturation, lightness, formattedAlpha);
return FormatHelper::format("hsla({}, {}, {}, {:.2f})", hue, saturation, lightness, alpha);
}

std::string Color::lch(bool includeAlpha)
std::string Color::lch(bool include_alpha)
{
const std::integral auto luminance = Number::integer(100);
const std::integral auto chroma = Number::integer(100);
const std::integral auto hue = Number::integer(360);
std::integral auto luminance = Number::integer(100);
std::integral auto chroma = Number::integer(100);
std::integral auto hue = Number::integer(360);

if (!includeAlpha)
if (!include_alpha)
{
return FormatHelper::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();
std::floating_point auto alpha = Number::decimal(1.0);

return FormatHelper::format("lcha({}, {}, {}, {})", luminance, chroma, hue, formattedAlpha);
return FormatHelper::format("lcha({}, {}, {}, {:.2f})", luminance, chroma, hue, alpha);
}

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.);
std::floating_point auto cyan = Number::decimal(1.);
std::floating_point auto magenta = Number::decimal(1.);
std::floating_point auto yellow = Number::decimal(1.);
std::floating_point auto key = Number::decimal(1.);

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);
std::floating_point auto lightness = Number::decimal(100.0);
std::floating_point auto red_green = Number::decimal(-128.0, 128.0);
std::floating_point auto blue_yellow = Number::decimal(-128.0, 128.0);

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

std::string Color::hsb()
{
const std::integral auto hue = Number::integer(360);
const std::integral auto saturation = Number::integer(100);
const std::integral auto brightness = Number::integer(100);
std::integral auto hue = Number::integer(360);
std::integral auto saturation = Number::integer(100);
std::integral auto brightness = Number::integer(100);

return FormatHelper::format("hsb({}, {}, {})", hue, saturation, brightness);
}

std::string Color::hsv()
{
const std::integral auto hue = Number::integer(360);
const std::integral auto saturation = Number::integer(100);
const std::integral auto value = Number::integer(100);
std::integral auto hue = Number::integer(360);
std::integral auto saturation = Number::integer(100);
std::integral auto value = Number::integer(100);

return FormatHelper::format("hsv({}, {}, {})", hue, saturation, value);
}

std::string Color::yuv()
{
const std::integral auto luminance = Number::integer(255);
const std::integral auto chrominanceBlueColor = Number::integer(255);
const std::integral auto chrominanceRedColor = Number::integer(255);
std::integral auto luminance = Number::integer(255);
std::integral auto chrominance_blue = Number::integer(255);
std::integral auto chrominance_red = Number::integer(255);

return FormatHelper::format("yuv({}, {}, {})", luminance, chrominanceBlueColor, chrominanceRedColor);
return FormatHelper::format("yuv({}, {}, {})", luminance, chrominance_blue, chrominance_red);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#pragma once

#include <string>
#include <vector>
#include "ColorData.h"

namespace faker
{
const std::vector<std::string> colors = {
const std::array<std::string_view, 31> colors = {
"red", "green", "blue", "yellow", "purple", "mint green", "teal", "white",
"black", "orange", "pink", "grey", "maroon", "violet", "turquoise", "tan",
"sky blue", "salmon", "plum", "orchid", "olive", "magenta", "lime", "ivory",
Expand Down
7 changes: 7 additions & 0 deletions src/modules/color/ColorData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <array>
#include <string_view>

namespace faker
{
extern const std::array<std::string_view, 31> colors;
}
4 changes: 2 additions & 2 deletions tests/modules/color/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

#include "gtest/gtest.h"

#include "color/ColorData.h"
#include "common/StringHelper.h"
#include "string/data/Characters.h"
#include "color/data/Colors.h"

using namespace ::testing;
using namespace faker;
Expand All @@ -21,7 +21,7 @@ TEST_F(ColorTest, shouldGenerateColorName)
{
const auto generatedColorName = Color::name();

ASSERT_TRUE(std::ranges::any_of(colors, [generatedColorName](const std::string& colorName)
ASSERT_TRUE(std::ranges::any_of(colors, [generatedColorName](const std::string_view& colorName)
{ return colorName == generatedColorName; }));
}

Expand Down
Loading