Results 1 to 6 of 6

Thread: sdlcolor.pas

  1. #1

    sdlcolor.pas

    Not sure if anyone else will find this usefull, but I plan to add it to JEDI-SDL...

    [pascal]
    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 ;
    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.
    [/pascal]

    Improvements, new colour definitions and other enhancements welcome.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  2. #2

    sdlcolor.pas

    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

    Dean
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  3. #3

    sdlcolor.pas

    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.


    Code:
    function fraction&#40;maxnew, maxold, val&#58; single&#41;&#58; single;
    begin
      if maxnew = 0 then
        Result &#58;= 0
      else
        Result &#58;= &#40;val * maxold&#41; / maxnew;
    end;
    
    function ColorLERP&#40;src, dest&#58; TColor; min, max, val&#58; single&#41;&#58; TColor;
    var
      interp&#58; integer;
    begin
    
      interp &#58;= trunc&#40;fraction&#40;max, 255, val&#41;&#41;;
    
      Result&#91;0&#93; &#58;= ClampB&#40;ClampB&#40;src&#91;0&#93; - interp&#41; + ClampB&#40;dest&#91;0&#93; - 255 + interp&#41;&#41;;
      Result&#91;1&#93; &#58;= ClampB&#40;ClampB&#40;src&#91;1&#93; - interp&#41; + ClampB&#40;dest&#91;1&#93; - 255 + interp&#41;&#41;;
      Result&#91;2&#93; &#58;= ClampB&#40;ClampB&#40;src&#91;2&#93; - interp&#41; + ClampB&#40;dest&#91;2&#93; - 255 + interp&#41;&#41;;
      Result&#91;3&#93; &#58;= ClampB&#40;ClampB&#40;src&#91;3&#93; - interp&#41; + ClampB&#40;dest&#91;3&#93; - 255 + interp&#41;&#41;;
    end;
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  4. #4

    sdlcolor.pas

    Quote Originally Posted by technomage
    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
    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.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  5. #5

    sdlcolor.pas

    Delfi, that looks interesting, but where is the ClampB function defined?
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  6. #6

    sdlcolor.pas

    Code:
    function ClampB&#40;n&#58; integer&#41;&#58; byte;
    begin
      if n > 255 then
        Result &#58;= 255
      else
      if n < 0 then
        Result &#58;= 0
      else
        Result &#58;= n;
    end;
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •