PDA

View Full Version : sdlcolor.pas



savage
12-11-2006, 05:06 PM
Not sure if anyone else will find this usefull, but I plan to add it to JEDI-SDL...


unit sdlcolor;

{$I jedi-sdl.inc}

interface

uses
sdl;

type
TColor = -$7FFFFFFF-1..$7FFFFFFF;

const
sclBlack : TSDL_Color = ( r : 0; g : 0; b : 0; unused : 0 );
sclMaroon : TSDL_Color = ( r : 128; g : 0; b : 0; unused : 0 );
sclGreen : TSDL_Color = ( r : 0; g : 128; b : 0; unused : 0 );
sclOlive : TSDL_Color = ( r : 128; g : 128; b : 0; unused : 0 );
sclNavy : TSDL_Color = ( r : 0; g : 0; b : 128; unused : 0 );
sclPurple : TSDL_Color = ( r : 128; g : 0; b : 128; unused : 0 );
sclTeal : TSDL_Color = ( r : 0; g : 128; b : 128; unused : 0 );
sclGray : TSDL_Color = ( r : 128; g : 128; b : 128; unused : 0 );
sclSilver : TSDL_Color = ( r : 192; g : 192; b : 192; unused : 0 );
sclRed : TSDL_Color = ( r : 255; g : 0; b : 0; unused : 0 );
sclLime : TSDL_Color = ( r : 0; g : 255; b : 0; unused : 0 );
sclYellow : TSDL_Color = ( r : 255; g : 255; b : 0; unused : 0 );
sclBlue : TSDL_Color = ( r : 0; g : 0; b : 255; unused : 0 );
sclFuchsia : TSDL_Color = ( r : 255; g : 0; b : 255; unused : 0 );
sclAqua : TSDL_Color = ( r : 0; g : 255; b : 255; unused : 0 );
sclWhite : TSDL_Color = ( r : 255; g : 255; b : 255; unused : 0 );
sclMoneyGreen : TSDL_Color = ( r : 192; g : 220; b : 192; unused : 0 );
sclSkyBlue : TSDL_Color = ( r : 166; g : 202; b : 240; unused : 0 );
sclCream : TSDL_Color = ( r : 255; g : 251; b : 240; unused : 0 );
sclMedGray : TSDL_Color = ( r : 160; g : 160; b : 164; unused : 0 );

function TColorToTSDLColor( aColor : TColor ) : TSDL_Color;
function TSDLColorToTColor( aColor : TSDL_Color ) : TColor;
function TSDLColorToString( aColor : TSDL_Color ) : String;
function StringToTSDLColor( aColor : String ) : TSDL_Color;

implementation

uses
SysUtils;

function GetRValue(rgb: Longword): Byte;
begin
Result := Byte(rgb);
end;

function GetGValue(rgb: Longword): Byte;
begin
Result := Byte(rgb shr 8);
end;

function GetBValue(rgb: Longword): Byte;
begin
Result := Byte(rgb shr 16);
end;

function TColorToTSDLColor( aColor : TColor ) : TSDL_Color;
begin
result.r := GetRValue( aColor );
result.g := GetGValue( aColor );
result.b := GetBValue( aColor );
result.unused := 0;
end;

function TSDLColorToTColor( aColor : TSDL_Color ) : TColor;
begin
result := aColor.r or ( aColor.g shl 8 ) or ( aColor.b shl 16 );
end;

function TSDLColorToString( aColor : TSDL_Color ) : String;
begin
result := Format( '%d,%d,%d,%d', [ aColor.r, aColor.g, aColor.b, aColor.unused ] );
end;

function StringToTSDLColor( aColor : String ) : TSDL_Color;
var
dx : integer;
ns : string;
tx : string;
begin
tx := aColor;

dx := Pos(',', tx) ;
ns := Copy( tx, 0, dx - 1);
result.r := StrToInt( ns );
tx := Copy( tx, dx + 1, Length( tx ) - 1 );
dx := Pos(',', tx) ;
ns := Copy( tx, 0, dx - 1);
result.g := StrToInt( ns );
tx := Copy( tx, dx + 1, Length( tx ) - 1 );
dx := Pos(',', tx) ;
ns := Copy( tx, 0, dx - 1);
result.b := StrToInt( ns );
tx := Copy( tx, dx + 1, Length( tx ) - 1 );
result.unused := StrToInt( tx );
end;

end.


Improvements, new colour definitions and other enhancements welcome.

technomage
12-11-2006, 05:35 PM
Hi Dom

looks Good, I would however make the string version of the color the #FFFFFFFF format, like html. Rather than a comma delimited one.

Just my 10 p worth :wink:

Dean

JernejL
12-11-2006, 06:11 PM
Hmm, maybe you can add this nice routine i wrote, it will, depending on range and value fade from one color to another, i use it to apply fading colors to particle system in my game.





function fraction(maxnew, maxold, val: single): single;
begin
if maxnew = 0 then
Result := 0
else
Result := (val * maxold) / maxnew;
end;

function ColorLERP(src, dest: TColor; min, max, val: single): TColor;
var
interp: integer;
begin

interp := trunc(fraction(max, 255, val));

Result[0] := ClampB(ClampB(src[0] - interp) + ClampB(dest[0] - 255 + interp));
Result[1] := ClampB(ClampB(src[1] - interp) + ClampB(dest[1] - 255 + interp));
Result[2] := ClampB(ClampB(src[2] - interp) + ClampB(dest[2] - 255 + interp));
Result[3] := ClampB(ClampB(src[3] - interp) + ClampB(dest[3] - 255 + interp));
end;

savage
12-11-2006, 07:09 PM
looks Good, I would however make the string version of the color the #FFFFFFFF format, like html. Rather than a comma delimited one.

Just my 10 p worth :wink:
Der Dean, I don't know what the hell I was thinking. For some strange reason I thought that comma delimited would be easier to parse :). Man I swear I'm getting thicker every year. I will change it to the HTML format.

savage
12-11-2006, 07:13 PM
Delfi, that looks interesting, but where is the ClampB function defined?

JernejL
12-11-2006, 08:30 PM
function ClampB(n: integer): byte;
begin
if n > 255 then
Result := 255
else
if n < 0 then
Result &#58;= 0
else
Result &#58;= n;
end;