/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Aug 21, 2005 */ package eu.moonlight3d.colour; /** * \package eu.moonlight3d.colour * * This is an assortment of utility classes for colour processing. * * This package is part of the core program. */ /** * A class representing colour values. It contains various methods * for converting colour values betweeen different standard colour * spaces and representations. * * @author gregor */ public class Colour implements Cloneable { public double red, green, blue, alpha; /** * Integer RGBA values used for storing discreet values, usually in the * range of 0-255 for 8 bit colour depth. * * @author gregor */ public class RGB { public int red; public int green; public int blue; public int alpha; } public Colour() { red=0; green=0; blue=0; alpha=0; } public Colour(double red, double green, double blue, double alpha) { this.red=red; this.green=green; this.blue=blue; this.alpha=alpha; } @Override public Colour clone() { Colour c=new Colour(); c.red=red; c.green=green; c.blue=blue; c.alpha=alpha; return c; } public void setFromLinearRGBA8(int r, int g, int b, int a) { red=r/255.0; green=g/255.0; blue=b/255.0; alpha=a/255.0; } public void setFromLinearRGBA8(RGB rgb) { setFromLinearRGBA8(rgb.red,rgb.green,rgb.blue,rgb.alpha); } /** * Conversion from sRGB to linear (CIE xy) color space for a * single coordinate. Formula taken from Wikipedia article * on sRGB. * * @param srgb the sRGB value for the color coordinate * @return linear floating point value in CIE coordinate system */ private double fromsRGB(int srgb) { if(srgb < .04045*255) { return srgb*(1/12.92/255); } else { return Math.pow((srgb+(255*.055))*(1/1.055/255), 2.4f); } } public void setFromsRGB8(RGB rgb) { red=fromsRGB(rgb.red); green=fromsRGB(rgb.green); blue=fromsRGB(rgb.blue); alpha=fromsRGB(rgb.alpha); } public void setFromsRGB8(int red, int green, int blue, int alpha) { this.red=fromsRGB(red); this.green=fromsRGB(green); this.blue=fromsRGB(blue); this.alpha=fromsRGB(alpha); } public RGB getLinearRGB8() { RGB rgb=new RGB(); rgb.red=(int)red*255; if(rgb.red>255) { rgb.red=255; } rgb.green=(int)green*255; if(rgb.green>255) { rgb.green=255; } rgb.blue=(int)blue*255; if(rgb.blue>255) { rgb.blue=255; } rgb.alpha=(int)alpha*255; if(rgb.alpha>255) { rgb.alpha=255; } return rgb; } /** * Convert a single color coodinate from linear (CIE xy) color space to * sRGB. Formula taken from Wikipedia article on sRGB. * * @param linear linear CIE xy space coordinate value * @return sRGB color space value of that coordinate */ private int tosRGB(double linear) { int value; if(linear<(0.04045/12.92)) { value=(int)Math.round(linear*255*12.92); } else { value=(int)Math.round(1.055*255*Math.pow(linear,1/2.4)-(255*.055)); } if(value>255) { value=255; } return value; } public RGB getsRGB8() { RGB rgb=new RGB(); rgb.red=tosRGB(red); rgb.green=tosRGB(green); rgb.blue=tosRGB(blue); rgb.alpha=tosRGB(alpha); return rgb; } }