色クラス

色クラスがあると何かと便利なので作ってみた。

struct color
{
    byte b;
    byte g;
    byte r;
    byte a;

    color() : b(0), g(0), r(0), a(0) { }
    color(byte r_, byte g_, byte b_, byte a_) : r(r_), g(g_), b(b_), a(a_) { }
    color(uint32 v)
    {
        // リトルエンディアン限定
        *reinterpret_cast<uint32*>(&b) = v;
    }
    color(const color& c)
    {
        argb() = c.argb();
    }

    color& operator=(const color& c)
    {
        *reinterpret_cast<uint32*>(this) = c.argb();
        return *this;
    }

    uint32& argb() { return *reinterpret_cast<uint32*>(&b); }
    const uint32& argb() const { return *reinterpret_cast<const uint32*>(&b); }

    static const color white;
    static const color black;
    static const color gray;
    static const color red;
    static const color green;
    static const color blue;
    static const color yellow;
    static const color magenta;
    static const color cyan;

    fixed fa() const { return fixed(a, 255); }
    fixed fr() const { return fixed(r, 255); }
    fixed fg() const { return fixed(g, 255); }
    fixed fb() const { return fixed(b, 255); }
};

良い子はマネしちゃだめよ、なキャストだらけ。
で、作った後に思い出したんだけど、特に ARM は 4 バイト境界じゃないとやばいので、

byte* buf = new byte[6]; // アロケートしたメモリは 4 バイトアラインされている
buf += 2; // ちょっと2バイトほどずらす
*reinterpret_cast<color*>(buf) = 0xffff00ff; // だめぽ。少なくとも期待した値にはならない。

こんなことが起こりうる。
作り直さないとなんだけど、面倒だし、どうせ color クラス自体は 4 バイトだから↑みたいなことはそんなに起こらないはず。
問題が起こったら作り直す方向で……。