急求HSB(HSV)转换为RGB的解决办法
如题!
最多只能给100分,如解决另给分!
问题点数:100、回复次数:6Top
1 楼unsigned(僵哥(发站内消息,请附上链接或问题说明,否则不予回复))回复于 2006-03-04 01:38:09 得分 100
uses Math;
type
TRGBColor = record
Red,
Green,
Blue : Byte;
end;
THSBColor = record
Hue,
Saturnation,
Brightness : Double;
end;
function RGBToHSB(rgb : TRGBColor) : THSBColor;
var
minRGB, maxRGB, delta : Double;
h , s , b : Double ;
begin
H := 0.0 ;
minRGB := Min(Min(rgb.Red, rgb.Green), rgb.Blue) ;
maxRGB := Max(Max(rgb.Red, rgb.Green), rgb.Blue) ;
delta := ( maxRGB - minRGB ) ;
b := maxRGB ;
if (maxRGB <> 0.0) then s := 255.0 * Delta / maxRGB
else s := 0.0;
if (s <> 0.0) then
begin
if rgb.Red = maxRGB then h := (rgb.Green - rgb.Blue) / Delta
else
if rgb.Green = minRGB then h := 2.0 + (rgb.Blue - rgb.Red) / Delta
else
if rgb.Blue = maxRGB then h := 4.0 + (rgb.Red - rgb.Green) / Delta
end
else h := -1.0;
h := h * 60 ;
if h < 0.0 then h := h + 360.0;
with result do
begin
Hue := h;
Saturnation := s * 100 / 255;
Brightness := b * 100 / 255;
end;
end;
请反向求取Top
2 楼unsigned(僵哥(发站内消息,请附上链接或问题说明,否则不予回复))回复于 2006-03-04 01:40:25 得分 0
另外找了一段C代码,不好意思,明天要外出客户那里,所以就不帮忙翻译了^_^
--------------------
// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v )
{
float min, max, delta;
min = MIN( r, g, b );
max = MAX( r, g, b );
*v = max; // v
delta = max - min;
if( max != 0 )
*s = delta / max; // s
else {
// r = g = b = 0 // s = 0, v is undefined
*s = 0;
*h = -1;
return;
}
if( r == max )
*h = ( g - b ) / delta; // between yellow & magenta
else if( g == max )
*h = 2 + ( b - r ) / delta; // between cyan & yellow
else
*h = 4 + ( r - g ) / delta; // between magenta & cyan
*h *= 60; // degrees
if( *h < 0 )
*h += 360;
}
void HSVtoRGB( float *r, float *g, float *b, float h, float s, float v )
{
int i;
float f, p, q, t;
if( s == 0 ) {
// achromatic (grey)
*r = *g = *b = v;
return;
}
h /= 60; // sector 0 to 5
i = floor( h );
f = h - i; // factorial part of h
p = v * ( 1 - s );
q = v * ( 1 - s * f );
t = v * ( 1 - s * ( 1 - f ) );
switch( i ) {
case 0:
*r = v;
*g = t;
*b = p;
break;
case 1:
*r = q;
*g = v;
*b = p;
break;
case 2
*r = p;
*g = v;
*b = t;
break;
case 3:
*r = p;
*g = q;
*b = v;
break;
case 4:
*r = t;
*g = p;
*b = v;
break;
default: // case 5:
*r = v;
*g = p;
*b = q;
break;
}
}
Top
3 楼heman2000(柳枫)回复于 2006-03-04 01:45:01 得分 0
已经解决,谢谢各位!
代码如下,希望大家以后有好代码不要吝啬。在别人的基础上修改的,有少许误差,但影响不大!
====================CODE--HEMAN========================
type
TRGBColor = record
Red,
Green,
Blue : integer;
end;
THSBColor = record
Hue,
Saturnation,
Brightness : Double;
end;
-----------------------------------------------------------
//HSB2RGB
function HSB2RGB(H: THSBColor): TRGBColor;
Var
HN,SN,LN,RD,GD,BD,V,M,SV,Fract,VSF,Mid1,Mid2:double;
R,G,B:Integer;
Sextant:integer;
begin
HN:=H.Hue/239;
SN:=H.Saturnation/240;
LN:=H.Brightness/240;
if LN<0.5 then
V:=LN*(1.0+SN)
else
V:=LN+SN-LN*SN;
if V<=0 then
begin
RD:=0.0;
GD:=0.0;
BD:=0.0;
end
else
begin
M:=LN+LN-V;
SV:=(V-M)/V;
HN:=HN*6.0;
Sextant:=Trunc(HN);
Fract:=HN-Sextant;
VSF:=V*SV*Fract;
Mid1:=M+VSF;
Mid2:=V-VSF;
case Sextant of
0:begin
RD:=V;
GD:=Mid1;
BD:=M;
end;
1:begin
RD:=Mid2;
GD:=V;
BD:=M;
end;
2:begin
RD:=M;
GD:=V;
BD:=Mid1;
end;
3:begin
RD:=M;
GD:=Mid2;
BD:=V;
end;
4:begin
RD:=Mid1;
GD:=M;
BD:=V;
end;
5:begin
RD:=V;
GD:=M;
BD:=Mid2;
end;
else
begin
RD:=V;
GD:=Mid1;
BD:=M;
end;
end; //end case
end; //end fi
if RD>1.0 then RD:=1.0;
if GD>1.0 then GD:=1.0;
if BD>1.0 then BD:=1.0;
R:=Round(RD*255);
G:=Round(GD*255);
B:=Round(BD*255);
result.Red:=R;
result.Green:=G;
result.Blue:=B;
end;Top
4 楼heman2000(柳枫)回复于 2006-03-04 02:57:22 得分 0
//RGB2HSB
function TForm1.RGB2HSB(R: TRGBColor): THSBColor;
Var
Dif,CCmax,CCmin,RC,GC,BC,TempH,TempS,TempL:double;
begin
RC:=R.Red/255.0;
GC:=R.Green/255.0;
BC:=R.Blue/255.0;
if RC>GC then CCmax:=RC else CCmax:=GC;
if BC>CCmax then CCmax:=BC;
if RC<GC then CCmin:=RC else CCmin:=GC;
if BC<CCmin then CCmin:=BC;
TempL:=(CCmax+CCmin)/2.0;
if CCmax=CCmin then
begin
TempS:=0;
TempH:=0;
end
else
begin
Dif:=CCmax-CCmin;
if TempL<0.5 then TempS:=Dif/(CCmax+CCmin) else TempS:=Dif/(2.0-CCmax-CCmin);
if RC=CCmax then
TempH:=(GC-BC)/Dif
else if GC=CCmax then TempH:=2.0+(BC-RC)/Dif else TempH:=4.0+(RC-GC)/Dif;
TempH:=TempH/6;
if TempH<0 then TempH:=TempH+1;
end;
result.Hue:=Round(240*TempH);
result.Saturnation:=Round(240*TempS);
result.Brightness:=Round(240*TempL);
end;
Top
5 楼heman2000(柳枫)回复于 2006-03-04 02:58:45 得分 0
互转后还是有少许差别呀,懒的细纠了。哈哈,交差了!Top
6 楼pillsen(弹丸子)回复于 2006-04-30 15:59:10 得分 0
不错的!
有没有yv12(yuv)转换为RGB的Delphi代码?
yv12(yuv)转换为RGB的解决办法 ,sdk中提供了视频流给出了yv12格式的文件并且给出参数有:pBuf:pChar;nSize,nWidth,nHeight,nStamp,nType,nReceaved:LongInt,现需要将yv12文件转为RGB,后再转BMP或jpg,请哪位大哥指点出路,能搞点Delphi代码出来...小弟感谢了!Top




