Skip to content

Commit

Permalink
refactor: change color class to namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal committed Jun 18, 2024
1 parent 9f9d1c3 commit 2d099d2
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 148 deletions.
217 changes: 106 additions & 111 deletions include/faker-cxx/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,125 +4,120 @@

#include "types/Hex.h"

namespace faker
namespace faker::color
{
class Color
{
public:
/**
* @brief Returns a random color.
*
* @returns Human readable color name.
*
* @code
* Color::name() // "Blue"
* @endcode
*/
static std::string_view name();
/**
* @brief Returns a random color.
*
* @returns Human readable color name.
*
* @code
* color::name() // "Blue"
* @endcode
*/
std::string_view name();

/**
* @brief Returns an RGB color.
*
* @param includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`.
*
* @returns RGB color formatted with rgb(X,X,X) or rgba(X,X,X,X).
*
* @code
* Color::rgb() // "rgb(67, 28, 240)"
* Color::rgb(true) // "rgba(220, 28, 79, 0.50)"
* @endcode
*/
static std::string rgb(bool includeAlpha = false);
/**
* @brief Returns an RGB color.
*
* @param includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`.
*
* @returns RGB color formatted with rgb(X,X,X) or rgba(X,X,X,X).
*
* @code
* color::rgb() // "rgb(67, 28, 240)"
* color::rgb(true) // "rgba(220, 28, 79, 0.50)"
* @endcode
*/
std::string rgb(bool includeAlpha = false);

/**
* @brief Returns a hex color.
*
* @param casing Casing of the generated string. Defaults to `HexCasing::Lower`.
* @param prefix Prefix for the generated string. Defaults to `HexPrefix::ZeroX`.
* @param includeAlpha Adds an alpha value to the color. Defaults to `false`.
*
* @returns Hex color formatted that starts with `0x` or `#`.
*
* @code
* Color::hex() // "#e3f380"
* Color::hex(HexCasing::Upper, HexPrefix::ZeroX, true) // "0xE3F3801A"
* @endcode
*/
static std::string hex(HexCasing casing = HexCasing::Lower, HexPrefix prefix = HexPrefix::Hash,
bool includeAlpha = false);
/**
* @brief Returns a hex color.
*
* @param casing Casing of the generated string. Defaults to `HexCasing::Lower`.
* @param prefix Prefix for the generated string. Defaults to `HexPrefix::ZeroX`.
* @param includeAlpha Adds an alpha value to the color. Defaults to `false`.
*
* @returns Hex color formatted that starts with `0x` or `#`.
*
* @code
* color::hex() // "#e3f380"
* color::hex(HexCasing::Upper, HexPrefix::ZeroX, true) // "0xE3F3801A"
* @endcode
*/
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);
/**
* @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
*/
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);
/**
* @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
*/
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();
/**
* @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
*/
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();
/**
* @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
*/
std::string lab();

/**
* @brief Return a HSB color
*
* @returns HSB color formatted with hsb(X,X,X)
* @code
* Color::hsb() // "hsb(37, 82, 50)"
* @endcode
*/
static std::string hsb();
/**
* @brief Return a HSB color
*
* @returns HSB color formatted with hsb(X,X,X)
* @code
* color::hsb() // "hsb(37, 82, 50)"
* @endcode
*/
std::string hsb();

/**
* @brief Return a HSV color
*
* @returns HSV color formatted with hsv(X,X,X)
* @code
* Color::hsv() // "hsv(21, 67, 39)"
* @endcode
*/
static std::string hsv();
/**
* @brief Return a HSV color
*
* @returns HSV color formatted with hsv(X,X,X)
* @code
* color::hsv() // "hsv(21, 67, 39)"
* @endcode
*/
std::string hsv();

/**
* @brief Return a YUV color
*
* @returns YUV color formatted with yuv(X,X,X)
* @code
* Color::yuv() // "yuv(42, 234, 109)"
* @endcode
*/
static std::string yuv();
};
/**
* @brief Return a YUV color
*
* @returns YUV color formatted with yuv(X,X,X)
* @code
* color::yuv() // "yuv(42, 234, 109)"
* @endcode
*/
std::string yuv();
}
22 changes: 11 additions & 11 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#include "faker-cxx/String.h"
#include "faker-cxx/types/Hex.h"

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

std::string Color::rgb(bool includeAlpha)
std::string rgb(bool includeAlpha)
{
const std::integral auto red = Number::integer(255);
const std::integral auto green = Number::integer(255);
Expand All @@ -33,12 +33,12 @@ std::string Color::rgb(bool includeAlpha)
return FormatHelper::format("rgba({}, {}, {}, {:.2f})", red, green, blue, alpha);
}

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

std::string Color::hsl(bool include_alpha)
std::string hsl(bool include_alpha)
{
std::integral auto hue = Number::integer(360);
std::integral auto saturation = Number::integer(100);
Expand All @@ -54,7 +54,7 @@ std::string Color::hsl(bool include_alpha)
return FormatHelper::format("hsla({}, {}, {}, {:.2f})", hue, saturation, lightness, alpha);
}

std::string Color::lch(bool include_alpha)
std::string lch(bool include_alpha)
{
std::integral auto luminance = Number::integer(100);
std::integral auto chroma = Number::integer(100);
Expand All @@ -70,7 +70,7 @@ std::string Color::lch(bool include_alpha)
return FormatHelper::format("lcha({}, {}, {}, {:.2f})", luminance, chroma, hue, alpha);
}

std::string Color::cmyk()
std::string cmyk()
{
std::floating_point auto cyan = Number::decimal(1.);
std::floating_point auto magenta = Number::decimal(1.);
Expand All @@ -80,7 +80,7 @@ std::string Color::cmyk()
return FormatHelper::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key);
}

std::string Color::lab()
std::string lab()
{
std::floating_point auto lightness = Number::decimal(100.0);
std::floating_point auto red_green = Number::decimal(-128.0, 128.0);
Expand All @@ -89,7 +89,7 @@ std::string Color::lab()
return FormatHelper::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, red_green, blue_yellow);
}

std::string Color::hsb()
std::string hsb()
{
std::integral auto hue = Number::integer(360);
std::integral auto saturation = Number::integer(100);
Expand All @@ -98,7 +98,7 @@ std::string Color::hsb()
return FormatHelper::format("hsb({}, {}, {})", hue, saturation, brightness);
}

std::string Color::hsv()
std::string hsv()
{
std::integral auto hue = Number::integer(360);
std::integral auto saturation = Number::integer(100);
Expand All @@ -107,7 +107,7 @@ std::string Color::hsv()
return FormatHelper::format("hsv({}, {}, {})", hue, saturation, value);
}

std::string Color::yuv()
std::string yuv()
{
std::integral auto luminance = Number::integer(255);
std::integral auto chrominance_blue = Number::integer(255);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/color/ColorData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <array>
#include <string_view>

namespace faker
namespace faker::color
{
const std::array<std::string_view, 31> colors = {
"red", "green", "blue", "yellow", "purple", "mint green", "teal", "white",
Expand Down
2 changes: 1 addition & 1 deletion src/modules/color/ColorData.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <array>
#include <string_view>

namespace faker
namespace faker::color
{
extern const std::array<std::string_view, 31> colors;
}
Loading

0 comments on commit 2d099d2

Please sign in to comment.