Source

dark-hammer / include / core / color.h

/***********************************************************************************
 * Copyright (c) 2012, Sepehr Taghdisian
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 *
 ***********************************************************************************/


#ifndef __COLOR_H__
#define __COLOR_H__

#include <math.h>

#include "types.h"
#include "mem-mgr.h"

struct ALIGN16 color
{
    union   {
        struct {
            fl32 r;
            fl32 g;
            fl32 b;
            fl32 a;
        };

        fl32 f[4];
    };
};

/* globals */
static const struct color g_color_blue = {0.0f, 0.0f, 1.0f, 1.0f};
static const struct color g_color_red = {1.0f, 0.0f, 0.0f, 1.0f};
static const struct color g_color_green = {0.0f, 1.0f, 0.0f, 1.0f};
static const struct color g_color_white = {1.0f, 1.0f, 1.0f, 1.0f};
static const struct color g_color_black = {0.0f, 0.0f, 0.0f, 1.0f};
static const struct color g_color_grey = {0.3f, 0.3f, 0.3f, 1.0f};
static const struct color g_color_yellow = {1.0f, 1.0f, 0.0f, 1.0f};

#ifdef __cplusplus
extern "C" {
#endif

INLINE bool_t color_isequal(const struct color* c1, const struct color* c2)
{
    return (c1->r == c2->r) && (c1->g == c2->g) && (c1->b == c2->b) && (c1->a == c2->a);
}

INLINE struct color* color_setf(struct color* c, fl32 r, fl32 g, fl32 b, fl32 a)
{
    c->r = r;
    c->g = g;
    c->b = b;
    c->a = a;
    return c;
}

/* rgba values are between 0~255 range */
INLINE struct color* color_seti(struct color* c, uint8 r, uint8 g, uint8 b, uint8 a)
{
    c->r = ((fl32)r)/255.0f;
    c->g = ((fl32)g)/255.0f;
    c->b = ((fl32)b)/255.0f;
    c->a = ((fl32)a)/255.0f;
    return c;
}

INLINE struct color* color_setc(struct color* r, const struct color* c)
{
    r->r = c->r;
    r->g = c->g;
    r->b = c->b;
    r->a = c->a;
    return r;
}

INLINE uint32 color_rgba_uint(const struct color* c)
{
    uint8 r = (uint8)(c->r*255.0f);
    uint8 g = (uint8)(c->g*255.0f);
    uint8 b = (uint8)(c->b*255.0f);
    uint8 a = (uint8)(c->a*255.0f);
    return (uint32)((((r)&0xff)<<24) | (((g)&0xff)<<16) | (((b)&0xff)<<8) | ((a)&0xff));
}

INLINE uint32 color_rgb_uint(const struct color* c)
{
    uint8 r = (uint8)(c->r*255.0f);
    uint8 g = (uint8)(c->g*255.0f);
    uint8 b = (uint8)(c->b*255.0f);
    return (uint32)((((r)&0xff)<<24) | (((g)&0xff)<<16) | (((b)&0xff)<<8) | 0xff);
}

INLINE struct color* color_tolinear(struct color* r, const struct color* c)
{
    r->r = c->r*c->r;
    r->g = c->g*c->g;
    r->b = c->b*c->b;
    r->a = c->a;
    return r;
}

INLINE struct color* color_togamma(struct color* r, const struct color* c)
{
    r->r = sqrtf(c->r);
    r->g = sqrtf(c->g);
    r->b = sqrtf(c->b);
    r->a = c->a;
    return r;
}

INLINE struct color* color_muls(struct color* r, const struct color* c, fl32 k)
{
    r->r = c->r * k;
    r->g = c->g * k;
    r->b = c->b * k;
    r->a = c->a;
    return r;
}

INLINE struct color* color_swizzle_abgr(struct color* r, const struct color* c)
{
    return color_setf(r, c->a, c->b, c->g, c->r);
}

#ifdef __cplusplus
}
#endif

#endif /* COLOR_H */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.