Hi All

Hi All,

Trying to convert a FMX TAlphaColor to a VCL TColor and having some issues.

TAlphacolor is a four byte integer with hex $AARRGGBB format (i.e. AA transparency value 0 [i.e 00] transparent, 255 [i.e. FF] solid), RR the red byte, GG the green byte and BB the blue byte.

The TColor is also a 4 byte integer $00BBGGRR. with a palette coding, then blue green red.

I've Googled and consulted the Delphi help and found little.

Is there a Delphi function I have missed or has anyone a code snippet to do this conversion. I've tried but am not smart enough to do the maths to get it working :( Have also read that byte shifting with shr, shl might be a possible solution.

Cheers,

Martin

Comments

  1. I have a function to convert TColor to TAlphaColor. Should be simple to swap it around to perform the opposite function.

    uses
    System.UITypes;

    function ColorToAlphaColor(const Value: TColor; const Alpha: Byte): TAlphaColor;
    var
    CREC: TColorRec;
    AREC: TAlphaColorRec;
    begin
    CREC.Color := Value;

    AREC.A := Alpha;
    AREC.B := CREC.B;
    AREC.G := CREC.G;
    AREC.R := CREC.R;

    Result := AREC.Color;
    end;

    ReplyDelete
  2. While Paul's reply is nicely to the point, if you're building components or full applications atop today's myriad of Object Pascal UI frameworks (from Delphi's Windows-only VCL to its cross-platform sibling, FireMonkey, to Lazarus' LCL, and now even directly for the Web using Pas2JS - pas2js.org - http://wiki.freepascal.org/pas2js), you may wish to consider using TMS' "open-source" FNC described here:
    - http://www.tmssoftware.biz/download/manuals/DevelopingUIcontrolsfor3frameworks.pdf
    presented here:
    - https://www.youtube.com/watch?v=BSaI4RPTrUQ
    first blogged about here:
    - https://www.tmssoftware.com/site/blog.asp?post=346
    and available for download here:
    - tmssoftware.net - www.tmssoftware.net/public/TMSFNCCustomControls.zip

    TMS FNC Framework unifies and streamlines cross-platform Object Pascal development by implementing a powerful abstraction layer that greatly simplifies creating custom user interface controls starting from a single code base. FNC-based components share methods, properties and events that are 100% identical regardless of the UI framework being used.

    In the references provided above, Bruno Fierens, Managing director of TMS Software, takes you step-by-step through building a sample slider control. Therein, per your original question, you will see the FNC's TTMSFNCGraphicsColor unit used to abstract differences in color handling and transparency.

    An additional benefit of the FNC is its ability to register identical class names for different UI frameworks under Delphi. Here, Delphi requires that components from differing UI frameworks live in different namespaces. As such, the FNC segments UI component class names for reuse by having VCL controls unit names prefixed with "VCL.", FMX controls unit names prefixed with "FMX.", and LCL controls prefixed with LCL (but without a '.' to support FPC versions older than v3.0).

    DISCLOSURE: In the references above, as TMS is a commercial entity, Bruno also gives an overview of the custom controls that have been created by TMS Software that are available for purchase on their site (tmssoftware.com). That said, the "base" FNC framework source code is available for "free" to build your own components. As I do not work for TMS personally (I simply love the FNC library), please contact them directly to determine what, if any, commercial limitations exist for FNC use.

    ReplyDelete
  3. Thx Paul. Hadnt looked at TAlphaColorRec and TColorRec just TAlphaColor and TColor. Will give the ...ColorRec a go. Also thx TMS for the info. Seems like TMS has it all covered

    ReplyDelete

Post a Comment