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.
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.