请给出HSI<->RGB的Pascal代码(请大家继续,如果不够分可以再加点!) (300分)

  • 主题发起人 主题发起人 wlmmlw
  • 开始时间 开始时间
W

wlmmlw

Unregistered / Unconfirmed
GUEST, unregistred user!
请就在下面贴子给出,谢谢!
 
c的 自己转换一下吧 实在读不懂我再转一个吧
int RgbToHsi(struct RgbTriadStruct *RgbTriadStructPtr,

struct HsiTriadStruct *HsiTriadStructPtr)

{

double R, G, B, Sum, Quotient;

double Radians, Angle, MinValue, MaxValue, TempDouble1, TempDouble2;

R = ((double) RgbTriadStructPtr->Red) / 255.0;

G = ((double) RgbTriadStructPtr->Green) / 255.0;

B = ((double) RgbTriadStructPtr->Blue) / 255.0;

Sum = R + G + B;

HsiTriadStructPtr->Intensity = Sum / 3.0;

MinValue = (R < G) ? R : G;

MinValue = (B < MinValue) ? B : MinValue;

MaxValue = (R > G) ? R : G;

MaxValue = (B > MaxValue) ? B : MaxValue;

if (HsiTriadStructPtr->Intensity < 0.00001)

HsiTriadStructPtr->Saturation = ZERO_SATURATION;

else

HsiTriadStructPtr->Saturation = 1.0 - (3.0 * MinValue) / Sum;

if (MinValue == MaxValue)

{

HsiTriadStructPtr->Hue = UNDEFINED_HUE;

HsiTriadStructPtr->Saturation = ZERO_SATURATION;

return 0;

}

TempDouble1 = (((R - G) + (R - B)) / 2.0);

TempDouble2 = (R - G)*(R - G) + (R - B)*(G - B);

Quotient = (TempDouble1 / sqrt(TempDouble2));

if (Quotient > 0.99999999999999999)

Radians = 0.0;

else if (Quotient < -0.99999999999999999)

Radians = 3.1415926535;

else

Radians = acos(TempDouble1 / sqrt(TempDouble2));

Angle = Radians * DEGREES_PER_RADIAN;

if (B > G)

HsiTriadStructPtr->Hue = 360.0 - Angle;

else

HsiTriadStructPtr->Hue = Angle;

return 0;

}

int HsiToRgb(struct HsiTriadStruct *HsiTriadStructPtr,

struct RgbTriadStruct *RgbTriadStructPtr)

{

double r, g, b, Hue, Fraction, MaxDouble;

if (HsiTriadStructPtr->Saturation == ZERO_SATURATION)

{

if (HsiTriadStructPtr->Hue == UNDEFINED_HUE)

{

RgbTriadStructPtr->Red =

RgbTriadStructPtr->Green =

RgbTriadStructPtr->Blue =

HsiTriadStructPtr->Intensity * 255.0 + 0.5;

} else

{

printf("/n/nHsiToRgb: Illegal HSI hue value (saturation == 0)");



printf("/nHue == %5.3f, Saturation = %5.3f, Intensity == %5.3f",



HsiTriadStructPtr->Hue,

HsiTriadStructPtr->Saturation,

HsiTriadStructPtr->Intensity);

}

return 0;

}

if (HsiTriadStructPtr->Hue > 0.0 &amp;&amp;

HsiTriadStructPtr->Hue <= 120.0 )

{

b = (1.0 - HsiTriadStructPtr->Saturation) / 3.0;

Hue = HsiTriadStructPtr->Hue;

Fraction = (HsiTriadStructPtr->Saturation *

cos(Hue / DEGREES_PER_RADIAN))

/

cos((60.0 - Hue) / DEGREES_PER_RADIAN);

r = (1.0 + Fraction) / 3.0;

g = 1.0 - (r + b);

} else if (HsiTriadStructPtr->Hue > 120.0 &amp;&amp;

HsiTriadStructPtr->Hue <= 240.0 )

{

Hue = HsiTriadStructPtr->Hue - 120.0;

r = (1.0 - HsiTriadStructPtr->Saturation) / 3.0;

Fraction = (HsiTriadStructPtr->Saturation *

cos(Hue / DEGREES_PER_RADIAN))

/

cos((60.0 - Hue) / DEGREES_PER_RADIAN);

g = (1.0 + Fraction) / 3.0;

b = 1.0 - (r + g);

} else

{



Hue=

HsiTriadStructPtr->Hue - 240.0;

g = (1.0 - HsiTriadStructPtr->Saturation) / 3.0;

Fraction = (HsiTriadStructPtr->Saturation *

cos(Hue / DEGREES_PER_RADIAN))

/

cos((60.0 - Hue) / DEGREES_PER_RADIAN);

b = (1.0 + Fraction) / 3.0;

r = 1.0 - (g + b);

}

MaxDouble = ((r > g) ? r : g);

MaxDouble = ((MaxDouble > b) ? MaxDouble : b);

if (3.0 * HsiTriadStructPtr->Intensity * MaxDouble > 1.0)

HsiTriadStructPtr->Intensity = (1.0 / (3.0 * MaxDouble));

r *= 255.0;

g *= 255.0;

b *= 255.0;

RgbTriadStructPtr->Red =

(USGC) ((3.0 * HsiTriadStructPtr->Intensity * r) + 0.5);

RgbTriadStructPtr->Green =

(USGC) ((3.0 * HsiTriadStructPtr->Intensity * g) + 0.5);

RgbTriadStructPtr->Blue =

(USGC) ((3.0 * HsiTriadStructPtr->Intensity * b) + 0.5);



return 0;

}
 
请注意,我要的是Pascal代码。谢谢!
 
还是看不懂!
请问以上代码能在Delphi6下通过吗?
 
uses
math;

//RGB2HSI
I:=(R+G+B)/3;
S:=1-3*min(R,G,B)/(R+G+B);
H:=arccos((((R-G)+(R-B))/2)/sqrt))R-G)*(R-G)-(R-B)*(G-B)));


 
这里有你要的东西!算法自己提取,其他的不用说了吧?
PROCEDURE HLStoRGB(CONST H,L,S: TReal; VAR R,G,B: TReal);
VAR
m1: TReal;
m2: TReal;

FUNCTION Value(CONST n1,n2: TReal; hue: TReal): TReal;
BEGIN
IF hue > 360.0
THEN hue := hue - 360.0
ELSE
IF hue < 0.0
THEN hue := hue + 360.0;

IF hue < 60.0
THEN RESULT := n1 + (n2 - n1)*hue / 60.0
ELSE
IF hue < 180
THEN RESULT := n2
ELSE
IF hue < 240.0
THEN RESULT := n1 + (n2-n1)*(240.0 - hue) / 60.0
ELSE RESULT := n1
END {Value};

BEGIN
IF L <= 0.5
THEN m2 := L * (L + S)
ELSE m2 := L + S - L*S;
m1 := 2.0 * L - m2;

IF S = 0.0
THEN BEGIN // achromatic -- no hue
IF IsNAN(H)
THEN BEGIN
R := L;
G := L;
B := L
END
ELSE RAISE EColorError.Create('HLStoRGB: S = 0 and H has a value');
END
ELSE BEGIN
// Chromatic case -- there is a hue
R := Value(m1, m2, H + 120.0);
G := Value(m1, m2, H);
B := Value(m1, m2, H - 120.0)
END
END {HLStoRGB};


// H = 0 to 360 (corresponding to 0..360 degrees around hexcone)
// L = 0.0 (shade of gray) to 1.0 (pure color)
// S = 0.0 (black) to 1.0 {white)
//
// R, G, B each in [0,1]
//
// Based on C Code in "Computer Graphics -- Principles and Practice,"
// Foley et al, 1996, p. 595.

PROCEDURE RGBToHLS (CONST R,G,B: TReal; VAR H,L,S: TReal);
VAR
Delta: TReal;
Max : TReal;
Min : TReal;
BEGIN
Max := MaxValue( [R, G, B] );
Min := MinValue( [R, G, B] );

L := (Max + Min) / 2.0; // Lightness

IF Max = Min // Achromatic case since r = g = b
THEN BEGIN
S := 0.0;
H := NAN; // Undefined
END
ELSE BEGIN
Delta := Max - Min;

IF L <= 0.5
THEN S := Delta / (Max + Min)
ELSE S := Delta / (2.0 - (Max + Min));

IF R = Max
THEN // degrees between yellow and magenta
H := (60.0*(G - B)) / Delta
ELSE
IF G = Max
THEN // degrees between cyan and yellow
H := 120.0 + (60.0*(B - R)) / Delta
ELSE
IF B = Max
THEN // degrees between magenta and cyan
H := 240.0 + (60.0*(R - G)) / Delta;

IF H < 0
THEN H := H + 360.0; // Keep in interval [0, 360)

END
END {RGBtoHLS};


// == HSV / RGB =======================================================
//
// Based on C Code in "Computer Graphics -- Principles and Practice,"
// Foley et al, 1996, p. 593.
//
// H = 0.0 to 360.0 (corresponding to 0..360 degrees around hexcone)
// NaN (undefined) for S = 0
// S = 0.0 (shade of gray) to 1.0 (pure color)
// V = 0.0 (black) to 1.0 (white)

PROCEDURE HSVtoRGB (CONST H,S,V: TReal; VAR R,G,B: TReal);
VAR
f : TReal;
i : INTEGER;
hTemp: TReal; // since H is CONST parameter
p,q,t: TReal;
BEGIN
IF S = 0.0 // color is on black-and-white center line
THEN BEGIN
IF IsNaN(H)
THEN BEGIN
R := V; // achromatic: shades of gray
G := V;
B := V
END
ELSE RAISE EColorError.Create('HSVtoRGB: S = 0 and H has a value');
END

ELSE BEGIN // chromatic color
IF H = 360.0 // 360 degrees same as 0 degrees
THEN hTemp := 0.0
ELSE hTemp := H;

hTemp := hTemp / 60; // h is now IN [0,6)
i := TRUNC(hTemp); // largest integer <= h
f := hTemp - i; // fractional part of h

p := V * (1.0 - S);
q := V * (1.0 - (S * f));
t := V * (1.0 - (S * (1.0 - f)));

CASE i OF
0: BEGIN R := V; G := t; B := p END;
1: BEGIN R := q; G := V; B := p END;
2: BEGIN R := p; G := V; B := t END;
3: BEGIN R := p; G := q; B := V END;
4: BEGIN R := t; G := p; B := V END;
5: BEGIN R := V; G := p; B := q END
END
END
END {HSVtoRGB};


// RGB, each 0 to 255, to HSV.
// H = 0.0 to 360.0 (corresponding to 0..360.0 degrees around hexcone)
// S = 0.0 (shade of gray) to 1.0 (pure color)
// V = 0.0 (black) to 1.0 {white)

// Based on C Code in "Computer Graphics -- Principles and Practice,"
// Foley et al, 1996, p. 592. Floating point fractions, 0..1, replaced with
// integer values, 0..255.

PROCEDURE RGBToHSV (CONST R,G,B: TReal; VAR H,S,V: TReal);
VAR
Delta: TReal;
Min : TReal;
BEGIN
Min := MinValue( [R, G, B] );
V := MaxValue( [R, G, B] );

Delta := V - Min;

// Calculate saturation: saturation is 0 if r, g and b are all 0
IF V = 0.0
THEN S := 0
ELSE S := Delta / V;

IF S = 0.0
THEN H := NAN // Achromatic: When s = 0, h is undefined
ELSE BEGIN // Chromatic
IF R = V
THEN // between yellow and magenta [degrees]
H := 60.0 * (G - B) / Delta
ELSE
IF G = V
THEN // between cyan and yellow
H := 120.0 + 60.0 * (B - R) / Delta
ELSE
IF B = V
THEN // between magenta and cyan
H := 240.0 + 60.0 * (R - G) / Delta;

IF H < 0.0
THEN H := H + 360.0
END
END {RGBtoHSV};


// == CMY / RGB =======================================================
//
// Based on C Code in "Computer Graphics -- Principles and Practice,"
// Foley et al, 1996, p. 588

// R, G, B, C, M, Y each IN [0.0 .. 1.0]
PROCEDURE CMYtoRGB(CONST C,M,Y: TReal; VAR R,G,B: TReal);
BEGIN
R := 1.0 - C;
G := 1.0 - M;
B := 1.0 - Y
END {CMYtoRGB};


// R, G, B, C, M, Y each IN [0.0 .. 1.0]
PROCEDURE RGBtoCMY(CONST R,G,B: TReal; VAR C,M,Y: TReal);
BEGIN
C := 1.0 - R;
M := 1.0 - G;
Y := 1.0 - B
END {RGBtoCMY};


// == CMYK / RGB ======================================================
//
// Based on C Code in "Computer Graphics -- Principles and Practice,"
// Foley et al, 1996, p. 589

// R, G, B, C, M, Y, K each IN [0.0 .. 1.0]
PROCEDURE CMYKtoRGB(CONST C,M,Y,K: TReal; VAR R,G,B: TReal);
BEGIN
R := 1.0 - (C + K);
G := 1.0 - (M + K);
B := 1.0 - (Y + K)
END {CMYtoRGB};


// R, G, B, C, M, Y each IN [0.0 .. 1.0]
PROCEDURE RGBToCMYK(CONST R,G,B: TReal; VAR C,M,Y,K: TReal);
BEGIN
RGBtoCMY(R,G,B, C,M,Y);
K := MinValue([C, M, Y]);
C := C - K;
M := M - K;
Y := Y - K
END {RGBtoCMYK};
 
对不起,我找不到 HSI !
 
要得点分可真不容易,辛苦了[:D]
 
分再多也不感兴趣了!
 
^_^ 不知您对什么感兴趣?
 
兄弟们,帮帮手!!!
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部