#ifndef COLORYIQ_H
#define COLORYIQ_H

#include <G3D/G3DAll.h>
#include <GLG3D/GLG3D.h>

/**
   @cite http://en.wikipedia.org/wiki/YIQ
*/
class ColorYIQ {
public:

    float y, i, q;

    ColorYIQ(const Color3& c) : 
        y(0.299f * c.r + 0.587f * c.g + 0.114f * c.b),
        i(0.5957161349127745527f * c.r - 0.2744528378392564636f * c.g - 0.3212632970735180891f * c.b),
        q(0.2114564021201178664f * c.r - 0.5225910452916111684 * c.g + 0.3111346431714933020 * c.b) {
    }

    ColorYIQ(float y, float i, float q) : y(y), i(i), q(q) {}

    ColorYIQ() : y(0), i(0), q(0) {}
    
    inline Color3 toColor3() const {
        return Color3(y + 0.9562948323208939905f * i + 0.6210251254447287141 * q,
                      y - 0.2721214740839773195 * i - 0.6473809535176157223 * q,
                      y - 1.106989908567128216 * i + 1.704614975498829329 * q);
    }

    inline float distanceSquared(const ColorYIQ& c) const {
        return square(y - c.y) + square(i - c.i) + square(q - c.q);
    }

    inline ColorYIQ operator+(const ColorYIQ& c) const {
        return ColorYIQ(y + c.y, i + c.i, q + c.q);
    }

    inline ColorYIQ operator*(const float f) const {
        return ColorYIQ(y * f, i * f, q * f);
    }

};

#endif
