Jump to content

Converting HexColor To ARGB


Adele
 Share

Go to solution Solved by UEZ,

Recommended Posts

I would like to write a text to image with _GDIPlus_BrushCreateSolid() function but it requires a ARGB color code. I want to use hex color codes. Is Converting HexColor To ARGB possible for this one?

Edited by Adele
Link to comment
Share on other sites

You can use e.g. _GDIPlus_BrushCreateSolid(0xFFABCDEF) to create a brush color using hex numbers whereas the format is 0xARGB .

I'm using always hex format because it is much more readable than using an intereger!

 

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Can I convert a Hex value to RGBA? I will write text to image with my function, I want to use Hex color codes for that. I don't want to use ARGBA, because it is not useful and friendly.

Func _Image_WriteText($text, $file, $save, $x = 0, $y = 0, $color = 0xff000000, $font = "Arial", $font_w = 30, $style = 0)
    Local $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen

    _GDIPlus_StartUp()

    $hImage   = _GDIPlus_ImageLoadFromFile($file)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hFamily  = _GDIPlus_FontFamilyCreate($font)
    $hFont    = _GDIPlus_FontCreate($hFamily, $font_w, 1)
    $hFormat  = _GDIPlus_StringFormatCreate(0x4000)
    $hBrush2  = _GDIPlus_BrushCreateSolid($color)
    $hPen     = _GDIPlus_PenCreate(0xC4000000, 1)
    $tLayout = _GDIPlus_RectFCreate ($x, $y)
    $aInfo    = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $text, $hFont, $aInfo[0], $hFormat, $hBrush2)

    _GDIPlus_ImageSaveToFile($hImage, $save)

    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_BrushDispose($hBrush2)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ShutDown()
EndFunc
Edited by Adele
Link to comment
Share on other sites

You mean instead of ARGB input format you want to use RGBA but you need to convert RGBA to ARGB.

Correct?

The best is you show any input format and the desired output format.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

You can do something like this here to convert a hex RGBA value to ARGB which is needed to create a brush color:
 

$iHex_Input_RGBA = 0x115599FF
$iHex_Input_ARGB = BitAND($iHex_Input_RGBA, 0xFF) * 0x1000000 + BitShift($iHex_Input_RGBA, 8)

ConsoleWrite(Hex($iHex_Input_ARGB, 8) & @CRLF)

Is this what you are looking for?

Btw, what is your mother tongue?

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

What do I do wrong? It doesn't work.

#include <GDIPlus.au3>

Func _Image_WriteText($text, $file, $save, $x = 0, $y = 0, $color_rgba = 0xFF0000, $font = "Arial", $font_w = 30, $style = 0)
    Local $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen

    $color_argb = BitAND($color_rgba, 0xFF) * 0x1000000 + BitShift($color_rgba, 8)

    _GDIPlus_StartUp()

    $hImage   = _GDIPlus_ImageLoadFromFile($file)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hFamily  = _GDIPlus_FontFamilyCreate($font)
    $hFont    = _GDIPlus_FontCreate($hFamily, $font_w, 1)
    $hFormat  = _GDIPlus_StringFormatCreate(0x4000)
    $hBrush2  = _GDIPlus_BrushCreateSolid($color_argb)
    $hPen     = _GDIPlus_PenCreate(0xC4000000, 1)
    $tLayout = _GDIPlus_RectFCreate ($x, $y)
    $aInfo    = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $text, $hFont, $aInfo[0], $hFormat, $hBrush2)

    _GDIPlus_ImageSaveToFile($hImage, $save)

    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_BrushDispose($hBrush2)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ShutDown()
EndFunc

;FF0000 is the red color code.

_Image_WriteText("AutoIt", @ProgramFilesDir & "\AutoIt3\Examples\Helpfile\Extras\AutoIt.bmp", @DesktopDir & "\text_image.bmp", 0, 0, 0xFF0000)
Link to comment
Share on other sites

Try this:

#include <GDIPlus.au3>

Func _Image_WriteText($text, $file, $save, $x = 0, $y = 0, $color_rgba = 0x000000FF, $font = "Arial", $font_w = 30, $style = 0)
    Local $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen

    $color_argb = BitAND($color_rgba, 0xFF) * 0x1000000 + BitShift($color_rgba, 8)
    _GDIPlus_StartUp()

    $hImage   = _GDIPlus_ImageLoadFromFile($file)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hFamily  = _GDIPlus_FontFamilyCreate($font)
    $hFont    = _GDIPlus_FontCreate($hFamily, $font_w, 1)
    $hFormat  = _GDIPlus_StringFormatCreate(0x4000)
    $hBrush2  = _GDIPlus_BrushCreateSolid($color_argb)
    $hPen     = _GDIPlus_PenCreate(0xC4000000, 1)
    $tLayout = _GDIPlus_RectFCreate ($x, $y)
    $aInfo    = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $text, $hFont, $aInfo[0], $hFormat, $hBrush2)

    _GDIPlus_ImageSaveToFile($hImage, $save)

    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_BrushDispose($hBrush2)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ShutDown()
EndFunc

;FF0000 is the red color code.

_Image_WriteText("AutoIt", @ProgramFilesDir & "\AutoIt3\Examples\Helpfile\Extras\AutoIt.bmp", @DesktopDir & "\text_image.bmp", 0, 0, 0x000000FF)
The color code has always four tuble (RGBA) -> _Image_WriteText("AutoIt", @ProgramFilesDir & "AutoIt3ExamplesHelpfileExtrasAutoIt.bmp", @DesktopDir & "text_image.bmp", 0, 0, 0x000000FF)

 

You said that your input is in format RGBA because it is easier and that was the reason for the conversion to the ARGB format!

 

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

I mean that was. Thank you.

#include <GDIPlus.au3>

Func _Image_WriteText($text, $file, $save, $x = 0, $y = 0, $color_hex = 0x000000, $font = "Arial", $font_w = 30, $style = 0)
    Local $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen

    $Blue = BitAND($color_hex, 0xFF)
    $Green = BitAND(BitShift($color_hex, 8), 0xFF)
    $Red = BitAND(BitShift($color_hex, 16), 0xFF)
    $color_rgba = $Blue & $Green & $Red
    $color_argb = BitAND($color_rgba, 0xFF) * 0x1000000 + BitShift($color_rgba, 8)

    _GDIPlus_StartUp()

    $hImage   = _GDIPlus_ImageLoadFromFile($file)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hFamily  = _GDIPlus_FontFamilyCreate($font)
    $hFont    = _GDIPlus_FontCreate($hFamily, $font_w, 1)
    $hFormat  = _GDIPlus_StringFormatCreate(0x4000)
    $hBrush2  = _GDIPlus_BrushCreateSolid($color_argb)
    $hPen     = _GDIPlus_PenCreate(0xC4000000, 1)
    $tLayout = _GDIPlus_RectFCreate ($x, $y)
    $aInfo    = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $text, $hFont, $aInfo[0], $hFormat, $hBrush2)

    _GDIPlus_ImageSaveToFile($hImage, $save)

    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_BrushDispose($hBrush2)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ShutDown()
EndFunc

_Image_WriteText("AutoIt", @ProgramFilesDir & "\AutoIt3\Examples\Helpfile\Extras\AutoIt.bmp", @DesktopDir & "\text_image.bmp", 0, 0, 0x5D4CF6)
Link to comment
Share on other sites

  • Solution

Sorry to say that but what you did is not correct.

What you try to achieve is to convert a RGB value to ARGB format (and not from RGBA to ARGB!) but your conversation is not correct!

Try this:

#include <GDIPlus.au3>

Func _Image_WriteText($text, $file, $save, $x = 0, $y = 0, $color_hex_rgb = 0x000000, $font = "Arial", $font_w = 30, $style = 0)
    Local $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen

    $Blue = BitAND($color_hex_rgb, 0xFF)
    $Green = BitAND(BitShift($color_hex_rgb, 8), 0xFF)
    $Red = BitAND(BitShift($color_hex_rgb, 16), 0xFF)
    $color_argb = 0xFF000000 + $Red * 0x10000 + $Green * 0x100 + $Blue
    _GDIPlus_StartUp()

    $hImage   = _GDIPlus_ImageLoadFromFile($file)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hFamily  = _GDIPlus_FontFamilyCreate($font)
    $hFont    = _GDIPlus_FontCreate($hFamily, $font_w, 1)
    $hFormat  = _GDIPlus_StringFormatCreate(0x4000)
    $hBrush2  = _GDIPlus_BrushCreateSolid($color_argb)
    $hPen     = _GDIPlus_PenCreate(0xC4000000, 1)
    $tLayout = _GDIPlus_RectFCreate ($x, $y)
    $aInfo    = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $text, $hFont, $aInfo[0], $hFormat, $hBrush2)

    _GDIPlus_ImageSaveToFile($hImage, $save)

    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_BrushDispose($hBrush2)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ShutDown()
EndFunc

_Image_WriteText("AutoIt", @ProgramFilesDir & "\AutoIt3\Examples\Helpfile\Extras\AutoIt.bmp", @DesktopDir & "\text_image.bmp", 0, 0, 0x5D4CF6)

Br,
UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...