Skip to content

Commit

Permalink
✨ λcommon v1.6.8 - Color enhancement.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Aug 25, 2018
1 parent 3d85308 commit 17d1fda
Show file tree
Hide file tree
Showing 11 changed files with 510 additions and 44 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ set(HEADERS_GRAPHICS include/lambdacommon/graphics/color.h)
set(HEADERS_FILESYSTEM include/lambdacommon/system/fs/filesystem.h)
set(HEADERS_EXCEPTIONS include/lambdacommon/exceptions/exceptions.h)
set(HEADERS_SYSTEM ${HEADERS_FILESYSTEM} include/lambdacommon/system/os.h include/lambdacommon/system/devices.h include/lambdacommon/system/terminal.h include/lambdacommon/system/system.h include/lambdacommon/system/uri.h)
set(HEADERS_BASE include/lambdacommon/lambdacommon.h include/lambdacommon/serializable.h include/lambdacommon/lstring.h include/lambdacommon/path.h include/lambdacommon/resources.h)
set(HEADERS_BASE include/lambdacommon/lambdacommon.h include/lambdacommon/serializable.h include/lambdacommon/lstring.h include/lambdacommon/path.h include/lambdacommon/resources.h include/lambdacommon/maths.h)
set(HEADERS_FILES ${HEADERS_CONNECTION} ${HEADERS_DOCUMENT} ${HEADERS_GRAPHICS} ${HEADERS_EXCEPTIONS} ${HEADERS_SYSTEM} ${HEADERS_BASE})
# There is the C++ sources files.
set(SOURCES_CONNECTION src/connection/address.cpp)
Expand Down
87 changes: 82 additions & 5 deletions include/lambdacommon/graphics/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
#define LAMBDACOMMON_COLOR_H

#include "../lambdacommon.h"
#include <utility>

namespace lambdacommon
{
using namespace std::rel_ops;

class LAMBDACOMMON_API Color
{
private:
Expand Down Expand Up @@ -89,32 +92,106 @@ namespace lambdacommon
*/
uint8_t alphaAsInt() const;

/*!
* Blends this color with a background color.
* @param bgColor The background color.
* @return The blended color.
*/
const Color blend(const Color &bgColor) const;

/*!
* Mixes this color with another color.
* @param b The other color.
* @param ratio The mix ratio.
* @return The mixed color.
*/
const Color mix(const Color &b, float ratio) const;

/*!
* Gets the color as an hexadecimal color.
* @return The hexadecimal color.
*/
uint64_t toHex() const;

/*!
* Gets the color as a string.
* @return The color as a string.
*/
std::string toString(bool hex = true) const;

bool operator==(const Color &other) const;

bool operator<(const Color &other) const;

const Color operator+(const Color &other) const;

/*!
* Subtracts the specified color to the current color. Alpha is not subtracted.
* @param other The color to subtract.
* @return The subtracted color.
*/
const Color operator-(const Color &other) const;

const Color operator*(const Color &other) const;

const Color operator*(float coefficient) const;

Color &operator+=(const Color &other);

Color &operator-=(const Color &other);

Color &operator*=(const Color &other);

/**
* Represents the black color.
*/
static Color BLACK;
static Color COLOR_BLACK;

/**
* Represents the white color.
*/
static Color WHITE;
static Color COLOR_WHITE;

/**
* Represents the red color.
*/
static Color RED;
static Color COLOR_RED;

/**
* Represents the green color.
*/
static Color GREEN;
static Color COLOR_GREEN;

/**
* Represents the blue color.
*/
static Color BLUE;
static Color COLOR_BLUE;
};

namespace color
{
/*!
* Blends two colors.
* @param fg The foreground color.
* @param bg The background color.
* @return The blended color.
*/
extern Color LAMBDACOMMON_API blend(const Color &fg, const Color &bg);

/*!
* Mixes two different colors with a ratio.
* @param a The first color to mix.
* @param b The second color to mix.
* @param ratio The mix ratio.
* @return The mixed color.
*/
extern Color LAMBDACOMMON_API mix(const Color &a, const Color &b, float ratio);

extern Color LAMBDACOMMON_API fromHex(uint64_t hexColor, bool hasAlpha = true);

extern Color LAMBDACOMMON_API fromHex(const std::string &hexColor);
}

extern Color LAMBDACOMMON_API getColorByIntRGBA(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255);
}

Expand Down
2 changes: 1 addition & 1 deletion include/lambdacommon/lambdacommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

#define LAMBDACOMMON_VERSION_MAJOR 1
#define LAMBDACOMMON_VERSION_MINOR 6
#define LAMBDACOMMON_VERSION_PATCH 7
#define LAMBDACOMMON_VERSION_PATCH 8

namespace lambdacommon
{
Expand Down
4 changes: 4 additions & 0 deletions include/lambdacommon/lstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ namespace lambdacommon

extern const std::string LAMBDACOMMON_API mergePath(std::string parent, const std::string &child);

extern int LAMBDACOMMON_API parseInt(const std::string &integer, int base = 10);

extern long LAMBDACOMMON_API parseLong(const std::string &longNumber, int base = 10);

#ifdef LAMBDA_WINDOWS

/**
Expand Down
123 changes: 123 additions & 0 deletions include/lambdacommon/maths.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright © 2018 AperLambda <aperlambda@gmail.com>
*
* This file is part of λcommon.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/

#ifndef LAMBDACOMMON_MATHS_H
#define LAMBDACOMMON_MATHS_H

#include "lambdacommon.h"

namespace lambdacommon
{
namespace maths
{
/*!
* Calculates the absolute value of a number (effectively removes any negative sign).
* @tparam N The number type.
* @param number The number to get the absolute value of.
* @return The absolute value.
*/
template<typename N>
N abs(N number)
{
return number < 0 ? -number : number;
}

/*!
* Returns the smallest value of all arguments.
* @tparam N The number type.
* @param a One of the numbers to get the smallest from.
* @param b One of the numbers to get the smallest from.
* @return The smallest number.
*/
template<typename N>
N min(N a, N b)
{
return a < b ? a : b;
}

/*!
* Returns the smallest value of all arguments.
* @tparam N The number type.
* @param numbers Numbers to get the smallest from.
* @return The smallest number.
*/
template<typename N>
N min(const std::initializer_list<N> &numbers)
{
if (numbers.size() == 0)
return 0;

bool first = true;
N minN;
for (auto i : numbers)
if (first)
{
minN = i;
first = false;
}
else
minN = min(minN, i);
return minN;
}

/*!
* Returns the largest value of all arguments.
* @tparam N The number type.
* @param a One of the numbers to get the largest from.
* @param b One of the numbers to get the largest from.
* @return The largest number.
*/
template<typename N>
N max(N a, N b)
{
return a > b ? a : b;
}

/*!
* Returns the largest value of all arguments.
* @tparam N The number type.
* @param numbers Numbers to get the largest from.
* @return The largest number.
*/
template<typename N>
N max(const std::initializer_list<N> &numbers)
{
if (numbers.size() == 0)
return 0;

bool first = true;
N maxN;
for (auto i : numbers)
if (first)
{
maxN = i;
first = false;
}
else
maxN = max(maxN, i);
return maxN;
}

/*!
* Clamps a number between a minimum and maximum value.
* @tparam N The number type.
* @param number The number to clamp.
* @param min The minimum value, this function will never return a number less than this.
* @param max The maximum value, this function will never return a number greater than this.
* @return The clamped value.
*/
template<typename N>
N clamp(N number, N min, N max)
{
return maths::min(maths::max(number, min), max);
}
}
}

#endif //LAMBDACOMMON_MATHS_H
3 changes: 3 additions & 0 deletions include/lambdacommon/system/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define LAMBDACOMMON_TERMINAL_H

#include "os.h"
#include "../graphics/color.h"
#include <vector>
#include <iostream>

Expand Down Expand Up @@ -88,6 +89,8 @@ namespace lambdacommon

extern std::ostream LAMBDACOMMON_API &operator<<(std::ostream &stream, std::vector<std::string> stringVector);

extern std::ostream LAMBDACOMMON_API &operator<<(std::ostream &stream, const Color &color);

/*!
* This function will erase the current line in the stream.
*
Expand Down
Loading

0 comments on commit 17d1fda

Please sign in to comment.