Skip to content

Instantly share code, notes, and snippets.

@masaha03
Created December 1, 2011 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masaha03/1416419 to your computer and use it in GitHub Desktop.
Save masaha03/1416419 to your computer and use it in GitHub Desktop.
HSY色空間変換
/*
http://sourceforge.jp/projects/hsy-color/wiki/HSY%E8%89%B2%E7%A9%BA%E9%96%93%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6
*/
private int RGBtoHSYtoRGB(int rgb) {
float h;
float y;
float s;
float S;
float MAX;
float MIN;
float MID;
// float pmax;
float pmin;
float pmid;
float f;
float r = (float) Color.red(rgb) / 255;
float g = (float) Color.green(rgb) / 255;
float b = (float) Color.blue(rgb) / 255;
float max = Math.max(Math.max(r, g), b);
float min = Math.min(Math.min(r, g), b);
float cr;
float cg;
float cb;
final float pr = 0.298912F;
final float pg = 0.586611F;
final float pb = 0.114478F;
//RGBからHSY
y = pr * r + pg * g + pb * b;
if (max == min){
h = 0;
s = 0;
} else {
s = (max - min) / max;
cr = (max - r) / (max - min);
cg = (max - g) / (max - min);
cb = (max - b) / (max - min);
h = r == max ? cb - cg : g == max ? 2 + cr - cb : 4 + cg - cr;
h = h * 60;
h += h < 0 ? 360 : 0;
}
y = 1F;
//HSVからRGB
int cond = (int) Math.floor((double) h / 60);
f = (h % 60F) / 60F;
switch(cond){
case 0:
// pmax = pr;
pmid = pg;
pmin = pb;
S = (1 - y)/(pmid - pmid*f + pmin);
S = S > 1 ? 1 : S;
MAX = y / (1 + S*((f - 1)*pmid - pmin));
MID = (1 - s + s*f)*MAX;
MIN = (1 - s)*MAX;
r = MAX;
g = MID;
b = MIN;
break;
case 1:
// pmax = pg;
pmid = pr;
pmin = pb;
S = (1 - y)/(pmid - pmid*(1 - f) + pmin);
S = S > 1 ? 1 : S;
MAX = y / (1 + S*(((1 - f) - 1)*pmid - pmin));
MID = (1 - s + s*(1 - f))*MAX;
MIN = (1 - s)*MAX;
g = MAX;
r = MID;
b = MIN;
break;
case 2:
// pmax = pg;
pmid = pb;
pmin = pr;
S = (1 - y)/(pmid - pmid*f + pmin);
S = S > 1 ? 1 : S;
MAX = y / (1 + S*((f - 1)*pmid - pmin));
MID = (1 - s + s*f)*MAX;
MIN = (1 - s)*MAX;
g = MAX;
b = MID;
r = MIN;
break;
case 3:
// pmax = pb;
pmid = pg;
pmin = pr;
S = (1 - y)/(pmid - pmid*(1 - f) + pmin);
S = S > 1 ? 1 : S;
MAX = y / (1 + S*(((1 - f) - 1)*pmid - pmin));
MID = (1 - s + s*(1 - f))*MAX;
// (1-f)*MAX
MIN = (1 - s)*MAX;
b = MAX;
g = MID;
r = MIN;
break;
case 4:
// pmax = pb;
pmid = pr;
pmin = pg;
S = (1 - y)/(pmid - pmid*f + pmin);
S = S > 1 ? 1 : S;
MAX = y / (1 + S*((f - 1)*pmid - pmin));
MID = (1 - s + s*f)*MAX;
MIN = (1 - s)*MAX;
b = MAX;
r = MID;
g = MIN;
break;
case 5:
// pmax = pr;
pmid = pb;
pmin = pg;
S = (1 - y)/(pmid - pmid*(1 - f) + pmin);
S = S > 1 ? 1 : S;
MAX = y / (1 + S*(((1 - f) - 1)*pmid - pmin));
MID = (1 - s + s*(1 - f))*MAX;
MIN = (1 - s)*MAX;
r = MAX;
b = MID;
g = MIN;
break;
}
int R = (int)(r*255);
int G = (int)(g*255);
int B = (int)(b*255);
return Color.rgb(R, G, B);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment