1
0
Fork 0
2018-soft-3d-renderer/include/color.h

116 lines
1.6 KiB
C
Raw Normal View History

#ifndef COLOR_H
#include "util.h"
#include <cstdint>
2018-09-19 05:33:52 +00:00
// STRUCTURES
struct ColorU32
{
union
{
struct
{
uint8_t b, g, r, a;
};
uint32_t u32;
};
};
struct ColorF32
{
float b, g, r, a;
};
// OPERATORS
// c1 + c2
2018-09-19 05:33:52 +00:00
inline ColorF32 operator+(ColorF32 const &c1, ColorF32 const &c2)
{
ColorF32 result;
2018-09-19 03:02:12 +00:00
result.b = c1.b + c2.b;
result.g = c1.g + c2.g;
result.r = c1.r + c2.r;
result.a = c1.a + c2.a;
return result;
}
// c1 += c2
2018-09-19 05:33:52 +00:00
inline ColorF32 &operator+=(ColorF32 &c1, ColorF32 const &c2)
{
c1 = c1 + c2;
2018-09-19 03:02:12 +00:00
return c1;
}
// c * f
2018-09-19 05:33:52 +00:00
inline ColorF32 operator*(ColorF32 const &c, float f)
{
ColorF32 result;
2018-09-19 03:02:12 +00:00
result.b = f * c.b;
result.g = f * c.g;
result.r = f * c.r;
result.a = f * c.a;
return result;
}
2018-09-11 02:51:59 +00:00
// f * c
2018-09-19 05:33:52 +00:00
inline ColorF32 operator*(float f, ColorF32 const &c)
2018-09-11 02:51:59 +00:00
{
ColorF32 result;
result = c * f;
return result;
}
2018-09-19 03:02:12 +00:00
// c1 * c2
2018-09-19 05:33:52 +00:00
inline ColorF32 operator*(ColorF32 const &c1, ColorF32 const &c2)
2018-09-19 03:02:12 +00:00
{
ColorF32 result;
result.b = c1.b * c2.b;
result.g = c1.g * c2.g;
result.r = c1.r * c2.r;
result.a = c1.a * c2.a;
return result;
}
// c / f
2018-09-19 05:33:52 +00:00
inline ColorF32 operator/(ColorF32 const &c, float f)
2018-09-19 03:02:12 +00:00
{
ColorF32 result;
float invF = 1.0f / f;
result.b = c.b * invF;
result.g = c.g * invF;
result.r = c.r * invF;
result.a = c.a * invF;
return result;
}
// c /= f
inline ColorF32 &operator/=(ColorF32 &c, float f)
{
c = c / f;
return c;
}
2018-09-19 05:33:52 +00:00
// PUBLIC FUNCTIONS
void ScaleColor(ColorF32 &c);
2018-09-19 03:02:12 +00:00
#define COLOR_H
#endif