Added Colour

This commit is contained in:
Diren D Bharwani 2022-10-13 03:03:21 +08:00
parent d95dbd5ce6
commit 381217a1b8
3 changed files with 464 additions and 0 deletions

View File

@ -0,0 +1,297 @@
/****************************************************************************************
* \file SHColour.cpp
* \author Diren D Bharwani, diren.dbharwani, 390002520
* \brief Implementation for a Colour.
*
* \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or
* disclosure of this file or its contents without the prior written consent
* of DigiPen Institute of Technology is prohibited.
****************************************************************************************/
#include <SHpch.h>
#include <DirectXColors.h>
// Primary Header
#include "SHColour.h"
// Project Headers
#include "SHMathHelpers.h"
using namespace DirectX;
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Static Data Member Definitions */
/*-----------------------------------------------------------------------------------*/
const SHColour SHColour::BEIGE = DirectX::Colors::Beige;
const SHColour SHColour::BLACK = DirectX::Colors::Black;
const SHColour SHColour::BLUE = DirectX::Colors::Blue;
const SHColour SHColour::BROWN = DirectX::Colors::Brown;
const SHColour SHColour::CHOCOLATE = DirectX::Colors::Chocolate;
const SHColour SHColour::CORAL = DirectX::Colors::Coral;
const SHColour SHColour::CRIMSON = DirectX::Colors::Crimson;
const SHColour SHColour::CYAN = DirectX::Colors::Cyan;
const SHColour SHColour::DARKBLUE = DirectX::Colors::DarkBlue;
const SHColour SHColour::DARKGRAY = DirectX::Colors::DarkGray;
const SHColour SHColour::DARKGREEN = DirectX::Colors::DarkGreen;
const SHColour SHColour::DARKMAGENTA = DirectX::Colors::DarkMagenta;
const SHColour SHColour::DARKORANGE = DirectX::Colors::DarkOrange;
const SHColour SHColour::DARKRED = DirectX::Colors::DarkRed;
const SHColour SHColour::DEEPPINK = DirectX::Colors::DeepPink;
const SHColour SHColour::FORESTGREEN = DirectX::Colors::ForestGreen;
const SHColour SHColour::FUCHSIA = DirectX::Colors::Fuchsia;
const SHColour SHColour::GOLD = DirectX::Colors::Gold;
const SHColour SHColour::GRAY = DirectX::Colors::Gray;
const SHColour SHColour::GREEN = DirectX::Colors::Green;
const SHColour SHColour::HOTPINK = DirectX::Colors::HotPink;
const SHColour SHColour::INDIGO = DirectX::Colors::Indigo;
const SHColour SHColour::LAVENDER = DirectX::Colors::Lavender;
const SHColour SHColour::LIGHTBLUE = DirectX::Colors::LightBlue;
const SHColour SHColour::LIGHTGRAY = DirectX::Colors::LightGray;
const SHColour SHColour::LIGHTGREEN = DirectX::Colors::LightGreen;
const SHColour SHColour::LIGHTPINK = DirectX::Colors::LightPink;
const SHColour SHColour::LIGHTYELLOW = DirectX::Colors::LightYellow;
const SHColour SHColour::LIME = DirectX::Colors::Lime;
const SHColour SHColour::LIMEGREEN = DirectX::Colors::LimeGreen;
const SHColour SHColour::MAGENTA = DirectX::Colors::Magenta;
const SHColour SHColour::MAROON = DirectX::Colors::Maroon;
const SHColour SHColour::MEDIUMBLUE = DirectX::Colors::MediumBlue;
const SHColour SHColour::MEDIUMPURPLE = DirectX::Colors::MediumPurple;
const SHColour SHColour::NAVY = DirectX::Colors::Navy;
const SHColour SHColour::OLIVE = DirectX::Colors::Olive;
const SHColour SHColour::ORANGE = DirectX::Colors::Orange;
const SHColour SHColour::ORCHID = DirectX::Colors::Orchid;
const SHColour SHColour::PINK = DirectX::Colors::Pink;
const SHColour SHColour::PURPLE = DirectX::Colors::Purple;
const SHColour SHColour::RED = DirectX::Colors::Red;
const SHColour SHColour::ROYALBLUE = DirectX::Colors::RoyalBlue;
const SHColour SHColour::SALMON = DirectX::Colors::Salmon;
const SHColour SHColour::SANDYBROWN = DirectX::Colors::SandyBrown;
const SHColour SHColour::SILVER = DirectX::Colors::Silver;
const SHColour SHColour::SKYBLUE = DirectX::Colors::SkyBlue;
const SHColour SHColour::SLATEGRAY = DirectX::Colors::SlateGray;
const SHColour SHColour::SNOW = DirectX::Colors::Snow;
const SHColour SHColour::STEELBLUE = DirectX::Colors::SteelBlue;
const SHColour SHColour::TAN = DirectX::Colors::Tan;
const SHColour SHColour::TEAL = DirectX::Colors::Teal;
const SHColour SHColour::TURQUOISE = DirectX::Colors::Turquoise;
const SHColour SHColour::VIOLET = DirectX::Colors::Violet;
const SHColour SHColour::WHITE = DirectX::Colors::White;
const SHColour SHColour::YELLOW = DirectX::Colors::Yellow;
/*-----------------------------------------------------------------------------------*/
/* Constructors & Destructor Definitions */
/*-----------------------------------------------------------------------------------*/
SHColour::SHColour() noexcept
: SHVec4 { 0.0f, 0.0f, 0.0f, 1.0f }
{}
SHColour::SHColour(float r, float g, float b) noexcept
: SHVec4 { r, g, b, 1.0f }
{}
SHColour::SHColour(float r, float g, float b, float a) noexcept
: SHVec4 { r, g, b, a }
{}
SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b) noexcept
: SHVec4
{
static_cast<float>(r) / 255.0f,
static_cast<float>(g) / 255.0f,
static_cast<float>(b) / 255.0f,
1.0f
}
{}
SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
: SHVec4
{
static_cast<float>(r) / 255.0f,
static_cast<float>(g) / 255.0f,
static_cast<float>(b) / 255.0f,
static_cast<float>(a) / 255.0f
}
{}
SHColour::SHColour(const DirectX::XMFLOAT3& colour) noexcept
: SHVec4 { colour.x, colour.y, colour.z, 1.0f }
{}
SHColour::SHColour(const DirectX::XMVECTORF32& colour) noexcept
: SHVec4
{
XMVectorGetX(colour),
XMVectorGetY(colour),
XMVectorGetZ(colour),
XMVectorGetW(colour)
}
{}
/*-----------------------------------------------------------------------------------*/
/* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/
bool SHColour::operator==(const SHColour& rhs) const noexcept
{
return XMColorEqual(*this, rhs);
}
bool SHColour::operator!=(const SHColour& rhs) const noexcept
{
return XMColorNotEqual(*this, rhs);
}
/*-----------------------------------------------------------------------------------*/
/* Function Member Definitions */
/*-----------------------------------------------------------------------------------*/
SHColourHSV SHColour::ToHSV() noexcept
{
SHColourHSV hsv;
const float MIN = SHMath::Min({ x, y, z });
const float MAX = SHMath::Max({ x, y, z });
hsv.v = MAX;
const float DELTA = MAX - MIN;
hsv.s = (MAX != 0.0f) ? DELTA / MAX : 0.0f;
static const float SIN_60 = sin(SHMath::DegreesToRadians(60.0f));
if (DELTA == 0.0f)
hsv.h = 0.0f;
else if (x == MAX)
hsv.h = (y - z) / DELTA;
else if (y == MAX)
hsv.h = 2.0f + (z - x) / DELTA;
else
hsv.h = 4.0f + (x - y) / DELTA;
hsv.h *= 60;
if (hsv.h < 0.0f)
hsv.h += 360.f;
return hsv;
}
void SHColour::Negate() noexcept
{
XMStoreFloat4(this, XMColorNegative(*this));
}
void SHColour::Saturate() noexcept
{
XMStoreFloat4(this, XMVectorSaturate(*this));
}
void SHColour::AdjustSaturation(float saturation) noexcept
{
XMStoreFloat4(this, XMColorAdjustSaturation(*this, saturation));
}
void SHColour::AdjustContrast(float contrast) noexcept
{
XMStoreFloat4(this, XMColorAdjustContrast(*this, contrast));
}
/*-----------------------------------------------------------------------------------*/
/* Static Function Member Definitions */
/*-----------------------------------------------------------------------------------*/
SHColour SHColour::Modulate(const SHColour& lhs, const SHColour& rhs) noexcept
{
SHColour result;
XMStoreFloat4(&result, XMColorModulate(lhs, rhs));
return result;
}
SHColour SHColour::FromHSV(float hue, float saturation, float value)
{
if (hue < 0.0f || saturation < 0.0f || value < 0.0f)
throw std::invalid_argument("One or more of the hsv values are invalid!");
SHColour colour;
if (saturation == 0.0f)
{
colour.x = colour.y = colour.z = value;
}
else
{
hue /= 60.0f;
const int SECTOR = static_cast<int>(hue);
const float F = hue - static_cast<float>(SECTOR);
const float P = value * (1.0f - saturation);
const float Q = value * (1.0f - saturation * F);
const float T = value * (1.0f - saturation * (1.0f - F));
switch (SECTOR)
{
case 0:
{
colour.x = value;
colour.y = T;
colour.z = P;
break;
}
case 1:
{
colour.x = Q;
colour.y = value;
colour.z = P;
break;
}
case 2:
{
colour.x = P;
colour.y = value;
colour.z = T;
break;
}
case 3:
{
colour.x = P;
colour.y = Q;
colour.z = value;
break;
}
case 4:
{
colour.x = T;
colour.y = P;
colour.z = value;
break;
}
default:
{
colour.x = value;
colour.y = P;
colour.z = Q;
break;
}
}
}
return colour;
}
SHColour SHColour::FromHSV(const SHColourHSV& hsv)
{
return FromHSV(hsv.h, hsv.s, hsv.v);
}
} // namespace SHADE

View File

@ -0,0 +1,166 @@
/****************************************************************************************
* \file SHColour.h
* \author Diren D Bharwani, diren.dbharwani, 390002520
* \brief Interface for a Colour.
*
* \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or
* disclosure of this file or its contents without the prior written consent
* of DigiPen Institute of Technology is prohibited.
****************************************************************************************/
#pragma once
#include <DirectXMath.h>
// Project Headers
#include "SH_API.h"
#include "Vector/SHVec3.h"
#include "Vector/SHVec4.h"
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Forward Declarations */
/*-----------------------------------------------------------------------------------*/
class SHColour;
/*-----------------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------------*/
struct SH_API SHColourHSV
{
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
float h = 0.0f;
float s = 0.0f;
float v = 0.0f;
};
class SH_API SHColour : private SHVec4
{
public:
/*---------------------------------------------------------------------------------*/
/* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/
SHColour () noexcept;
SHColour (float r, float g, float b) noexcept;
SHColour (float r, float g, float b, float a) noexcept;
SHColour (uint8_t r, uint8_t g, uint8_t b) noexcept;
SHColour (uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept;
SHColour (const SHVec3& colour) noexcept;
SHColour (const DirectX::XMFLOAT3& colour) noexcept;
SHColour (const DirectX::XMVECTORF32& colour) noexcept;
SHColour (const SHColour&) = default;
SHColour (SHColour&&) = default;
~SHColour () = default;
/*---------------------------------------------------------------------------------*/
/* Operator Overloads */
/*---------------------------------------------------------------------------------*/
SHColour& operator= (const SHColour&) = default;
SHColour& operator= (SHColour&&) = default;
bool operator== (const SHColour& rhs) const noexcept;
bool operator!= (const SHColour& rhs) const noexcept;
/*---------------------------------------------------------------------------------*/
/* Properties */
/*---------------------------------------------------------------------------------*/
[[nodiscard]] inline float& r() noexcept { return x; }
[[nodiscard]] inline float& g() noexcept { return y; }
[[nodiscard]] inline float& b() noexcept { return z; }
[[nodiscard]] inline float& a() noexcept { return w; }
/*---------------------------------------------------------------------------------*/
/* Function Members */
/*---------------------------------------------------------------------------------*/
SHColourHSV ToHSV () noexcept;
void Negate () noexcept;
void Saturate () noexcept;
void AdjustSaturation (float saturation) noexcept;
void AdjustContrast (float contrast) noexcept;
/*---------------------------------------------------------------------------------*/
/* Static Function Members */
/*---------------------------------------------------------------------------------*/
[[nodiscard]] static SHColour Modulate (const SHColour& lhs, const SHColour& rhs) noexcept;
[[nodiscard]] static SHColour FromHSV (float hue, float saturation, float value);
[[nodiscard]] static SHColour FromHSV (const SHColourHSV& hsv);
/*---------------------------------------------------------------------------------*/
/* Static Data Members */
/*---------------------------------------------------------------------------------*/
static const SHColour BEIGE ;
static const SHColour BLACK ;
static const SHColour BLUE ;
static const SHColour BROWN ;
static const SHColour CHOCOLATE ;
static const SHColour CORAL ;
static const SHColour CRIMSON ;
static const SHColour CYAN ;
static const SHColour DARKBLUE ;
static const SHColour DARKGRAY ;
static const SHColour DARKGREEN ;
static const SHColour DARKMAGENTA ;
static const SHColour DARKORANGE ;
static const SHColour DARKRED ;
static const SHColour DEEPPINK ;
static const SHColour FORESTGREEN ;
static const SHColour FUCHSIA ;
static const SHColour GOLD ;
static const SHColour GRAY ;
static const SHColour GREEN ;
static const SHColour HOTPINK ;
static const SHColour INDIGO ;
static const SHColour LAVENDER ;
static const SHColour LIGHTBLUE ;
static const SHColour LIGHTGRAY ;
static const SHColour LIGHTGREEN ;
static const SHColour LIGHTPINK ;
static const SHColour LIGHTYELLOW ;
static const SHColour LIME ;
static const SHColour LIMEGREEN ;
static const SHColour MAGENTA ;
static const SHColour MAROON ;
static const SHColour MEDIUMBLUE ;
static const SHColour MEDIUMPURPLE;
static const SHColour NAVY ;
static const SHColour OLIVE ;
static const SHColour ORANGE ;
static const SHColour ORCHID ;
static const SHColour PINK ;
static const SHColour PURPLE ;
static const SHColour RED ;
static const SHColour ROYALBLUE ;
static const SHColour SALMON ;
static const SHColour SANDYBROWN ;
static const SHColour SILVER ;
static const SHColour SKYBLUE ;
static const SHColour SLATEGRAY ;
static const SHColour SNOW ;
static const SHColour STEELBLUE ;
static const SHColour TAN ;
static const SHColour TEAL ;
static const SHColour TURQUOISE ;
static const SHColour VIOLET ;
static const SHColour WHITE ;
static const SHColour YELLOW;
};
} // namespace SHADE

View File

@ -23,6 +23,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/
/* Static Data Member Definitions */
/*-----------------------------------------------------------------------------------*/
SHVec3 const SHVec3::Zero { 0.0f, 0.0f, 0.0f };
SHVec3 const SHVec3::One { 1.0f, 1.0f, 1.0f };
SHVec3 const SHVec3::Left { -1.0f, 0.0f, 0.0f };