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

86 lines
1.3 KiB
C
Raw Normal View History

#ifndef COLOR_H
#include "util.h"
#include <cstdint>
struct ColorU32
{
union
{
struct
{
uint8_t b, g, r, a;
};
uint32_t u32;
};
};
struct ColorF32
{
float b, g, r, a;
static inline ColorU32 ConvertToU32(ColorF32 c)
{
ColorU32 result;
result.b = (uint8_t)(c.b * 255);
result.g = (uint8_t)(c.g * 255);
result.r = (uint8_t)(c.r * 255);
result.a = (uint8_t)(c.a * 255);
return result;
}
};
// OPERATORS
// c1 + c2
inline ColorF32 operator+(ColorF32 c1, ColorF32 c2)
{
ColorF32 result;
result.b = MIN((c1.b + c2.b), 1.0f);
result.g = MIN((c1.g + c2.g), 1.0f);
result.r = MIN((c1.r + c2.r), 1.0f);
result.a = MIN((c1.a + c2.a), 1.0f);
return result;
}
// c1 += c2
inline ColorF32 &operator+=(ColorF32 &c1, ColorF32 c2)
{
c1 = c1 + c2;
return(c1);
}
// c * f
inline ColorF32 operator*(ColorF32 c, float f)
{
ColorF32 result;
result.b = MIN((f * c.b), 1.0f);
result.g = MIN((f * c.g), 1.0f);
result.r = MIN((f * c.r), 1.0f);
result.a = MIN((f * c.a), 1.0f);
return result;
}
2018-09-11 02:51:59 +00:00
// f * c
inline ColorF32 operator*(float f, ColorF32 c)
{
ColorF32 result;
result = c * f;
return result;
}
#define COLOR_H
#endif