/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 */

package com.sun.rain.graphics;

public class Color
{
    static native void _initIDs();
    static
    {
        System.loadLibrary("rain");
        _initIDs();
    }
    
    static final public Color TRANSLUCENT = new Color(0x00000000);
    static final public Color TRANSPARENT = TRANSLUCENT;
    
    static final public Color WHITE = new Color(0xffffffff);
    static final public Color BLACK = new Color(0xff000000);
    
    static final public Color RED = new Color(0xffff0000);
    static final public Color GREEN = new Color(0xff00ff00);
    static final public Color BLUE = new Color(0xff0000ff);
    
    static final public Color DARK_GREY = new Color(0xff3f3f3f);
    static final public Color GREY = new Color(0xff7f7f7f);
    static final public Color LIGHT_GREY = new Color(0xffafafaf);

    static final public Color YELLOW = new Color(0xffffff00);

    private long ptr; // set by native code
    
    private float alpha;
    private float red;
    private float green;
    private float blue;
    
    native private void _createColor(float a, float r, float g, float b);
    public Color(int argb)
    {
        this(((argb>>24) & (0xff)), ((argb>>16) & (0xff)), ((argb>>8) & (0xff)), ((argb>>0) & (0xff)));
    }
    public Color(int r, int g, int b)
    {
        this(255, r, g, b);
    }
    public Color(int a, int r, int g, int b)
    {
        this(((float)a/255.0f), ((float)r/255.0f), ((float)g/255.0f), ((float)b/255.0f));
    }
    public Color(float r, float g, float b)
    {
        this(1.0f, r, g, b);
    }
    public Color(final float alpha, final float red, final float green, final float blue)
    {
        this.alpha = alpha;
        this.red = red;
        this.green = green;
        this.blue = blue;

        clamp();
        
        _createColor(this.alpha, this.red, this.green, this.blue);
    }
    
    public float getAlpha()
    {
        return this.alpha;
    }

    public float getRed()
    {
        return this.red;
    }

    public float getGreen()
    {
        return this.green;
    }

    public float getBlue()
    {
        return this.blue;
    }
    
    public int argb()
    {
        byte a = (byte)(getAlpha()*255.0);
        byte r = (byte)(getRed()*255.0);
        byte g = (byte)(getGreen()*255.0);
        byte b = (byte)(getBlue()*255.0);
        return ((a<<24) | (r<<16) | (g<<8) | b);
    }
    
    void clamp()
    {
        if (this.alpha < 0.0f)
        {
            this.alpha = 0.0f;
        }
        else if (this.alpha > 1.0f)
        {
            this.alpha = 1.0f;
        }

        if (this.red < 0.0f)
        {
            this.red = 0.0f;
        }
        else if (this.red > 1.0f)
        {
            this.red = 1.0f;
        }

        if (this.green < 0.0f)
        {
            this.green = 0.0f;
        }
        else if (this.green > 1.0f)
        {
            this.green = 1.0f;
        }

        if (this.blue < 0.0f)
        {
            this.blue = 0.0f;
        }
        else if (this.blue > 1.0f)
        {
            this.blue = 1.0f;
        }
    }
}
