Added Color struct

This commit is contained in:
Kah Wei 2022-10-28 13:53:05 +08:00
parent 392ecae434
commit 7d6af884a4
4 changed files with 471 additions and 1 deletions

View File

@ -0,0 +1,154 @@
/************************************************************************************//*!
\file Color.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Nov 3, 2021
\brief Contains the definition of the functions of the managed Color struct.
Note: This file is written in C++17/CLI.
Copyright (C) 2021 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.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "Graphics/Color.hxx"
// Standard Libraries
#include <algorithm>
// Project Includes
#include "Math/Math.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
Color::Color(float _red)
: Color { _red, 0.0f, 0.0f, 1.0f }
{}
Color::Color(float _red, float _green)
: Color { _red, _green, 0.0f, 1.0f }
{}
Color::Color(float _red, float _green, float _blue)
: Color { _red, _green, _blue, 1.0f }
{}
Color::Color(float _red, float _green, float _blue, float _alpha)
: r { _red }
, g { _green }
, b { _blue }
, a { _alpha }
{}
/*---------------------------------------------------------------------------------*/
/* IEquatable */
/*---------------------------------------------------------------------------------*/
bool Color::Equals(Object^ o)
{
try
{
Color col = safe_cast<Color>(o);
return Equals(col);
}
catch (System::InvalidCastException^)
{
return false;
}
}
/*---------------------------------------------------------------------------------*/
/* Object Overrides */
/*---------------------------------------------------------------------------------*/
bool Color::Equals(Color other)
{
return Math::CompareFloat(this->r, other.r)
&&
Math::CompareFloat(this->g, other.g)
&&
Math::CompareFloat(this->b, other.b)
&&
Math::CompareFloat(this->a, other.a);
}
int Color::GetHashCode()
{
const int HASH = 19;
const int HASH2 = 23;
const int HASH3 = 29;
return r.GetHashCode() * HASH
+ g.GetHashCode() * HASH2
+ b.GetHashCode() * HASH3
+ a.GetHashCode();
}
/*---------------------------------------------------------------------------------*/
/* Static Functions */
/*---------------------------------------------------------------------------------*/
Color Color::Lerp(Color colA, Color colB, float t)
{
return LerpUnclamped(colA, colB, std::clamp(t, 0.0f, 1.0f));
}
Color Color::LerpUnclamped(Color colA, Color colB, float t)
{
return colA + ((colB - colA) * t);
}
Color Color::operator+(Color lhs, Color rhs)
{
return Color
(
lhs.r + rhs.r,
lhs.g + rhs.g,
lhs.b + rhs.b,
lhs.a + rhs.a
);
}
Color Color::operator-(Color lhs, Color rhs)
{
return Color
(
lhs.r - rhs.r,
lhs.g - rhs.g,
lhs.b - rhs.b,
lhs.a - rhs.a
);
}
Color Color::operator*(Color lhs, Color rhs)
{
return Color
(
lhs.r * rhs.r,
lhs.g * rhs.g,
lhs.b * rhs.b,
lhs.a * rhs.a
);
}
Color Color::operator*(Color lhs, float rhs)
{
return Color
(
lhs.r * rhs,
lhs.g * rhs,
lhs.b * rhs,
lhs.a * rhs
);
}
Color Color::operator/(Color lhs, float rhs)
{
return Color
(
lhs.r / rhs,
lhs.g / rhs,
lhs.b / rhs,
lhs.a / rhs
);
}
bool Color::operator==(Color lhs, Color rhs)
{
return lhs.Equals(rhs);
}
bool Color::operator!=(Color lhs, Color rhs)
{
return !(lhs == rhs);
}
}

View File

@ -0,0 +1,287 @@
/************************************************************************************//*!
\file Color.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 28, 2022
\brief Contains the definition of the managed Color struct with the
declaration of functions for working with it.
Note: This file is written in C++17/CLI.
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
namespace SHADE
{
/// <summary>
/// CLR version of the the SHADE Engine's Color struct which describes a Color
/// encoded using floating point numbers that range from 0.0f to 1.0f.
/// </summary>
[System::Runtime::InteropServices::StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential)]
public value struct Color : public System::IEquatable<Color>
{
public:
/*-----------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// A static class that contains a set of default Colors.
/// </summary>
ref class Defaults abstract sealed
{
public:
/*-------------------------------------------------------------------------*/
/* Properties */
/*-------------------------------------------------------------------------*/
/// <summary>
/// Pure black.
/// </summary>
static property Color Black
{
Color get() { return Color(0.0f, 0.0f, 0.0f); }
}
/// <summary>
/// Light Gray, lighter than gray.
/// </summary>
static property Color LightGray
{
Color get() { return Color(0.827451f, 0.827451f, 0.827451f); }
}
/// <summary>
/// Gray, halfway between black and white.
/// </summary>
static property Color Gray
{
Color get() { return Color(0.5f, 0.5f, 0.5f); }
}
/// <summary>
/// Dark Gray, darker than gray.
/// </summary>
static property Color DarkGray
{
Color get() { return Color(0.622f, 0.622f, 0.622f); }
}
/// <summary>
/// Pure white.
/// </summary>
static property Color White
{
Color get() { return Color(1.0f, 1.0f, 1.0f); }
}
/// <summary>
/// Pure red.
/// </summary>
static property Color Red
{
Color get() { return Color(1.0f, 0.0f, 0.0f); }
}
/// <summary>
/// Pure green.
/// </summary>
static property Color Green
{
Color get() { return Color(0.0f, 1.0f, 0.0f); }
}
/// <summary>
/// Pure blue.
/// </summary>
static property Color Blue
{
Color get() { return Color(0.0f, 0.0f, 1.0f); }
}
/// <summary>
/// Pure cyan, mix of pure green and blue.
/// </summary>
static property Color Cyan
{
Color get() { return Color(0.0f, 1.0f, 1.0f); }
}
/// <summary>
/// Pure magenta, mix of pure red and blue.
/// </summary>
static property Color Magenta
{
Color get() { return Color(1.0f, 0.0f, 1.0f); }
}
/// <summary>
/// Pure yellow, mix of pure red and green.
/// </summary>
static property Color Yellow
{
Color get() { return Color(1.0f, 1.0f, 0.0f); }
}
};
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor to construct a Color with the specified components with the
/// green, blue and alpha component set to 1.0f.
/// </summary>
/// <param name="_red">Red component to set.</param>
Color(float _red);
/// <summary>
/// Constructor to construct a Color with the specified components with the
/// blue and alpha component set to 1.0f.
/// </summary>
/// <param name="_red">Red component to set.</param>
/// <param name="_green">Green component to set.</param>
Color(float _red, float _green);
/// <summary>
/// Constructor to construct a Color with the specified components with the
/// alpha component set to 1.0f.
/// </summary>
/// <param name="_red">Red component to set.</param>
/// <param name="_green">Green component to set.</param>
/// <param name="_blue">Blue component to set.</param>
Color(float _red, float _green, float _blue);
/// <summary>
/// Constructor to construct a Color with the specified components.
/// </summary>
/// <param name="_red">Red component to set.</param>
/// <param name="_green">Green component to set.</param>
/// <param name="_blue">Blue component to set.</param>
/// <param name="_alpha">Alpha component to set.</param>
Color(float _red, float _green, float _blue, float _alpha);
/*-----------------------------------------------------------------------------*/
/* Public Members */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Red component of the colour. Ranges from 0.0f to 1.0f.
/// </summary>
float r;
/// <summary>
/// Green component of the colour. Ranges from 0.0f to 1.0f.
/// </summary>
float g;
/// <summary>
/// Blue component of the colour. Ranges from 0.0f to 1.0f.
/// </summary>
float b;
/// <summary>
/// Alpha component of the colour. Ranges from 0.0f to 1.0f.
/// </summary>
float a;
/*-----------------------------------------------------------------------------*/
/* IEquatable */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Compares equality with an object of the same type.
/// </summary>
/// <param name="other">The object to compare with.</param>
/// <returns>True if both objects are the same.</returns>
virtual bool Equals(Color other);
/*-----------------------------------------------------------------------------*/
/* Object */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Compares equality with another unboxed object.
/// </summary>
/// <param name="o">The unboxed object to compare with.</param>
/// <returns>True if both objects are the same.</returns>
bool Equals(Object^ o) override;
/// <summary>
/// Gets a unique hash for this object.
/// </summary>
/// <returns>Unique hash for this object.</returns>
int GetHashCode() override;
/*-----------------------------------------------------------------------------*/
/* Static Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Linearly interpolates between two specified points.
/// This is most commonly used to find a point some fraction of the way along a
/// line between two endpoints.
/// </summary>
/// <param name="colA">The start Color, returned when t = 0.0.</param>
/// <param name="colB">The end Color, returned when t = 1.0.</param>
/// <param name="t">
/// Value used to interpolate between a and b which is clamped to
/// the range[0, 1].
/// </param>
/// <returns>The interpolated Vector3.</returns>
static Color Lerp(Color colA, Color colB, float t);
/// <summary>
/// Linearly interpolates between two specified points.
/// This is most commonly used to find a point some fraction of the way along a
/// line between two endpoints.
/// Unlike Lerp(), t is not clamped to a range at all.
/// </summary>
/// <param name="colA">The start Color, returned when t = 0.0.</param>
/// <param name="colB">The end Color, returned when t = 1.0.</param>
/// <param name="t">Value used to interpolate between a and b.</param>
/// <returns>The interpolated Color.</returns>
static Color LerpUnclamped(Color colA, Color colB, float t);
/*-----------------------------------------------------------------------------*/
/* Overloaded Operators */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Adds two Colors together and returns the result.
/// </summary>
/// <param name="lhs">Color to add.</param>
/// <param name="rhs">Another Color to add.</param>
/// <returns>The result of lhs added to rhs</returns>
static Color operator+(Color lhs, Color rhs);
/// <summary>
/// Subtracts a Color from another Color and returns the result.
/// </summary>
/// <param name="lhs">Color to subtract from.</param>
/// <param name="rhs">Another Color to subtract.</param>
/// <returns>The result of rhs subtracted from lhs.</returns>
static Color operator-(Color lhs, Color rhs);
/// <summary>
/// Calculates the component-wise multiplication of two Colors and returns the
/// result.
/// </summary>
/// <param name="lhs">Color to multiply with.</param>
/// <param name="rhs">Another Color to multiply with.</param>
/// <returns>The result of rhs subtracted from lhs.</returns>
static Color operator*(Color lhs, Color rhs);
/// <summary>
/// Calculates the multiplication of a Color with a scalar value and returns
/// the result.
/// </summary>
/// <param name="lhs">Color to multiply with.</param>
/// <param name="rhs">Scalar to multiply with.</param>
/// <returns>The result of the scalar multiplication.</returns>
static Color operator*(Color lhs, float rhs);
/// <summary>
/// Calculates the division of a Color with a scalar value and returns
/// the result.
/// </summary>
/// <param name="lhs">Scalar to divide with.</param>
/// <param name="rhs">Color to divide with.</param>
/// <returns>The result of the scalar division.</returns>
static Color operator/(Color lhs, float rhs);
/// <summary>
/// Checks if two Colors are approximately equal.
/// </summary>
/// <param name="lhs">Color to compare.</param>
/// <param name="rhs">Another Color to compare.</param>
/// <returns>
/// True if all components are approximately equal within the default
/// tolerance value.
/// </returns>
static bool operator==(Color lhs, Color rhs);
/// <summary>
/// Checks if two Colors are not approximately equal.
/// </summary>
/// <param name="lhs">Color to compare.</param>
/// <param name="rhs">Another Color to compare.</param>
/// <returns>
/// True if all components are not approximately equal within the default
/// tolerance value.
/// </returns>
static bool operator!=(Color lhs, Color rhs);
};
}

View File

@ -54,4 +54,15 @@ namespace SHADE
{ {
return (value - a) / (b - a); return (value - a) / (b - a);
} }
bool Math::CompareFloat(float a, float b)
{
return CompareFloat(a, b, Epsilon);
}
bool Math::CompareFloat(float a, float b, float tolerance)
{
return System::MathF::Abs(a - b) < tolerance;
}
} }

View File

@ -81,12 +81,30 @@ namespace SHADE
/// <returns>The interpolated float result between the two float values.</returns> /// <returns>The interpolated float result between the two float values.</returns>
static float LerpUnclamped(float a, float b, float t); static float LerpUnclamped(float a, float b, float t);
/// <summary> /// <summary>
/// Calculates the linear parameter t that produces the interpolant value within the range [a, b]. /// Calculates the linear parameter t that produces the interpolant value within
/// the range [a, b].
/// </summary> /// </summary>
/// <param name="a">Start value.</param> /// <param name="a">Start value.</param>
/// <param name="b">End value.</param> /// <param name="b">End value.</param>
/// <param name="value">Value between start and end.</param> /// <param name="value">Value between start and end.</param>
/// <returns>Percentage of value between start and end.</returns> /// <returns>Percentage of value between start and end.</returns>
static float InverseLerp(float a, float b, float value); static float InverseLerp(float a, float b, float value);
/// <summary>
/// Compares if two float values are close enough to be the same with a tolerance
/// of Epsilon.
/// </summary>
/// <param name="a">One of the values to compare.</param>
/// <param name="b">The other value to compare.</param>
/// <returns>True if a and b are practically the same.</returns>
static bool CompareFloat(float a, float b);
/// <summary>
/// Compares if two float values are close enough to be the same with the
/// specified tolerance value.
/// </summary>
/// <param name="a">One of the values to compare.</param>
/// <param name="b">The other value to compare.</param>
/// <param name="tolerance">Tolerance for floating point comparison.</param>
/// <returns>True if a and b are practically the same.</returns>
static bool CompareFloat(float a, float b, float tolerance);
}; };
} }