Sei sulla pagina 1di 8

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author User
*/

public class color {

private int red;


private int blue;
private int green;
private int RGB;
public static int m1;

private int Alpha;


public final static color White=new color(255,255,255);
public final static color Blue=new color(0,0,255);
public final static color Red=new color(255,0,0);
public final static color Green=new color(0,255,0);
public final static color Gray=new color(128,128,128);
public final static color Yellow=new color(255,255,0);
public final static color Black=new color(0,0,0);

public final static int bitmask=0;


public final static int opaque=1;
public final static int translucent=3;
public color()
{

public color(int r,int g,int b)


{
red=r;
blue=b;
green=g;
}
public color(float r,float g,float b)
{
red=(int) r;
blue=(int) b;
green=(int)g;
}

public int getRed()


{
return red;
}
public int getBlue()
{
return blue;
}
public int getGreen()
{
return green;
}
public int getRGB()
{
return RGB;
}
public int getAlpha()
{

Alpha=(getRGB()>>24) & 0xff;

return Alpha;

public static color getColor(String nm,int v)


{
color boja=new color();

boja=decode(nm);
if(boja==null)
{
boja.red=(v>>16);
boja.green=(v>>8)&0xFF;
boja.blue=v&0xFF;
String s="0x"+boja.getAlpha()+boja.red+boja.green+boja.blue;
boja=decode(s);
}
return boja;
}

public static color getColor(String nm)


{
color col=new color();
col=decode(nm);
return col;
}
public static color decode(String hexColor)
{
color c=new color();
c.RGB=Integer.decode(hexColor);
String r=hexColor.substring(2,4);
String g=hexColor.substring(4,6);
String b=hexColor.substring(6,8);
c.red=Integer.parseInt(r,16);
c.green=Integer.parseInt(g,16);
c.blue=Integer.parseInt(b,16);

return c;
}

public static float [] RGBtoHSB(int red,int green,int blue,float []hsbcode)


{
if (hsbcode == null)
hsbcode = new float[3];
// Calculate brightness.
int min;
int max;
if (red < green)
{
min = red;
max = green;
}
else
{
min = green;
max = red;
}
if (blue > max)
max = blue;
else if (blue < min)
min = blue;
hsbcode[2] = max / 255f;
// Calculate saturation.
if (max == 0)
hsbcode[1] = 0;
else
hsbcode[1] = ((float) (max - min)) / ((float) max);
// Calculate hue.
if (hsbcode[1] == 0)
hsbcode[0] = 0;
else
{
float delta = (max - min) * 6;
if (red == max)
hsbcode[0] = (green - blue) / delta;
else if (green == max)
hsbcode[0] = 1f / 3 + (blue - red) / delta;
else
hsbcode[0] = 2f / 3 + (red - green) / delta;
if (hsbcode[0] < 0)
hsbcode[0]++;
}
return hsbcode;
}
public static float[] rgbtohsl(int red,int green,int blue,float[]ahsl)
{
if(ahsl==null)
{
ahsl=new float[3];

float r = red / 255f;


float g = green/ 255f;
float b = blue / 255f;
float max=0;
float min=0;
if(r>g && g>b)
{
max=r;
}
if(g>r && g>b)
{
max=g;
}
if(b>r && b>g)
{
max=b;
}
if(r<g && g<b)
{
min=r;
}
if(g<r && g<b)
{
min=g;
}
if(b<r && b<g)
{
min=b;
}

float h, s, l;
l = (max + min) / 2.0f;

if (max == min) {
h = s = 0.0f;
} else {
float d = max - min;
s = (l > 0.5f) ? d / (2.0f - max - min) : d / (max + min);

if (r > g && r > b)


h = (g - b) / d + (g < b ? 6.0f : 0.0f);

else if (g > b)
h = (b - r) / d + 2.0f;

else
h = (r - g) / d + 4.0f;

h /= 6.0f;
}
ahsl[0]=h*360;
ahsl[1]=s*100;
ahsl[2]=l*100;

return ahsl;
}
public static float[] rgbtocmyk(int red,int green,int blue, float[] acmyk)
{
if(acmyk==null)
{
acmyk=new float[4];
}
float c=1-(red/255f);
float m=1-(green/255f);
float y=1-(blue/255f);

float var_k=1.0f;
if(c<var_k)
{
var_k=c;
}
if(m<var_k)
{
var_k=m;
}
if(y<var_k)
{
var_k=y;
}
if(var_k==1)
{
c=0.0f;
m=0.0f;
y=0.0f;
}
else
{
c=(c-var_k)/(1-var_k);
m=(m-var_k)/(1-var_k);
y=(y-var_k)/(1-var_k);

}
float k=var_k;

acmyk[0]=c;
acmyk[1]=m;
acmyk[2]=y;
acmyk[3]=k;
return acmyk;

public color brighter()


{
float fra=0.25f;

int r=getRed();
int g=getGreen();
int b=getBlue();
if(r==0 && g==0 && b==0)
{
r=3;
g=3;
b=3;

}
else
{
r=(int) (r+(255*fra));
g=(int) (g+(255*fra));
b=(int) (b+(255*fra));
}
return new color(r,g,b);

}
public color darker()
{
float fra=0.25f;

int r=getRed();
int g=getGreen();
int b=getBlue();
if(r==0 && g==0 && b==0)
{
return new color(r,g,b);
}
else
{
r=(int) (r-(255*fra));
g=(int) (g-(255*fra));
b=(int) (b-(255*fra));
}
return new color(r,g,b);

}
public String tostring()
{
String s=getClass().getName()+"[r="+getRed()+"]"+"[g="+getGreen()
+"]"+"[b="+getBlue()+"]";
return s;
}
public int getTransparency()
{
int alpha=getAlpha();
switch (alpha) {
case 255:
return opaque;
case 0:
return bitmask;
default:
return translucent;
}

@Override
public int hashCode()
{
int result=17;
int hash=0;
hash=31*result+red;
hash=31*result+green;
hash=31*result+blue;
hash=31*result+RGB;

return hash;
}

public boolean equals(color obj) {

try
{
obj=(color)obj;

catch(Exception e)
{
e.getMessage();

if(this.getRed()==obj.getRed() && this.getGreen()==obj.getGreen() &&


this.getBlue()==obj.getBlue())
{
return true;
}
else
return false;
}
public static String HsbToRgb(float H, float S, float V) {

float R, G, B;

H /= 360f;
S /= 100f;
V /= 100f;
if (S == 0)
{
R = V * 255;
G = V * 255;
B = V * 255;
} else {
float var_h = H * 6;
if (H == 6)
var_h = 0;
int var_i = (int) Math.floor((double) var_h);

float var_1 = V * (1 - S);


float var_2 = V * (1 - S * (var_h - var_i));
float var_3 = V * (1 - S * (1 - (var_h - var_i)));

float var_r;
float var_g;
float var_b;
switch (var_i) {
case 0:
var_r = V;
var_g = var_3;
var_b = var_1;
break;
case 1:
var_r = var_2;
var_g = V;
var_b = var_1;
break;
case 2:
var_r = var_1;
var_g = V;
var_b = var_3;
break;
case 3:
var_r = var_1;
var_g = var_2;
var_b = V;
break;
case 4:
var_r = var_3;
var_g = var_1;
var_b = V;
break;
default:
var_r = V;
var_g = var_1;
var_b = var_2;
break;
}

R = var_r * 255; // RGB results from 0 to 255


G = var_g * 255;
B = var_b * 255;
}

String rs = Integer.toHexString((int) (R));


String gs = Integer.toHexString((int) (G));
String bs = Integer.toHexString((int) (B));

if (rs.length() == 1)
rs = "0" + rs;
if (gs.length() == 1)
gs = "0" + gs;
if (bs.length() == 1)
bs = "0" + bs;
return "#" + rs + gs + bs;
}

Potrebbero piacerti anche