// one line to give the program's name and an idea of what it does.
// Copyright (C) 2005 name of Borja Snchez Zamorano
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// Code from Gimp migrated to c-sharp
namespace Gimp{
public class ColorSpace {
public static RGB HsvToRgb(HSV hsv)
{
RGB rgb = new RGB();
int i;
double f, w, q, t;
double hue;
if (hsv.S == 0.0) {
rgb.R = hsv.V;
rgb.G = hsv.V;
rgb.B = hsv.V;
}
else {
hue = hsv.H;
if (hue == 1.0)
hue = 0.0;
hue *= 6.0;
i = (int) hue;
f = hue - i;
w = hsv.V * (1.0 - hsv.S);
q = hsv.V * (1.0 - (hsv.S * f));
t = hsv.V * (1.0 - (hsv.S * (1.0 - f)));
switch (i) {
case 0:
rgb.R = hsv.V;
rgb.G = t;
rgb.B = w;
break;
case 1:
rgb.R = q;
rgb.G = hsv.V;
rgb.B = w;
break;
case 2:
rgb.R = w;
rgb.G = hsv.V;
rgb.B = t;
break;
case 3:
rgb.R = w;
rgb.G = q;
rgb.B = hsv.V;
break;
case 4:
rgb.R = t;
rgb.G = w;
rgb.B = hsv.V;
break;
case 5:
rgb.R = hsv.V;
rgb.G = w;
rgb.B = q;
break;
}
}
return rgb;
}
public static HSV RgbToHsv(RGB rgb)
{
double max, min, delta;
HSV hsv = new HSV();
max = System.Math.Max(System.Math.Max(rgb.R, rgb.G), rgb.B);
min = System.Math.Min(System.Math.Min(rgb.R, rgb.G), rgb.B);
hsv.V = max;
delta = max - min;
if (delta > 0.0001) {
hsv.S = delta / max;
if (rgb.R == max) {
hsv.H = (rgb.G - rgb.B) / delta;
}
else if (rgb.G == max) {
hsv.H = 2.0 + (rgb.B - rgb.R) / delta;
}
else if (rgb.B == max) {
hsv.H = 4.0 + (rgb.R - rgb.G) / delta;
}
hsv.H /= 6.0;
if (hsv.H < 0.0)
hsv.H += 1.0;
else if (hsv.H > 1.0)
hsv.H -= 1.0;
}
else {
hsv.S = 0.0;
hsv.H = 0.0;
}
return hsv;
}
}
public class HSV {
double h,s,v;
public double H {
get {
return h;
}
set {
h = value;
}
}
public double S {
get {
return s;
}
set {
s = value;
}
}
public double V {
get {
return v;
}
set {
v = value;
}
}
public double GetH()
{
return System.Math.Round(h*359+0.5);
}
public double GetS()
{
return System.Math.Round(s*255+0.5);
}
public double GetV()
{
return v;
}
}
public class RGB {
double r,g,b;
public double R {
get {
return r;
}
set {
r = value;
}
}
public double G {
get {
return g;
}
set {
g = value;
}
}
public double B {
get {
return b;
}
set {
b = value;
}
}
public double GetR()
{
return r;
}
public double GetG()
{
return g;
}
public double GetB()
{
return b;
}
}
}
|