Jump to content



Photo

String color conversion


  • Please log in to reply
11 replies to this topic

#1 CaptainClucks

CaptainClucks

    Unum Cavillator Spuria

  • Active Members
  • PipPipPipPipPipPip
  • 1,216 posts

Posted 29 June 2012 - 10:41 AM

I've searched and cannot find a solution.

I am attempting to convert some color values from RGB to BGR, obviously it's harder to do this when the color is fetched as a string from a file.

a(Dec(Hex(BinaryMid(0xFF0000, 1, 3)))); turns into a blue color well b(Dec(Hex(BinaryMid("FF0000", 1, 3)))); mocks me as it turns into an ugly poop color and adding the "0x" just makes it red... Sleep(3000) Func a($a) GUICreate("") GUISetBkColor($a) GUISetState() EndFunc Func b($b) GUICreate("") GUISetBkColor($b) GUISetState() EndFunc

Edited by ApudAngelorum, 29 June 2012 - 10:41 AM.






#2 JohnOne

JohnOne

    John

  • Active Members
  • PipPipPipPipPipPip
  • 8,857 posts

Posted 29 June 2012 - 10:50 AM

http://autoitscript.com/forum/topic/140291-solved-bitwise-op/page__hl__%2Brgb+%2Bbgr
  • CaptainClucks likes this
AutoIt Absolute Beginners Require a serial
Run('hh mk:@MSITStore:'&StringReplace(@AutoItExe,'.exe','.chm')&'::/html/tutorials/helloworld/helloworld.htm','',@SW_MAXIMIZE)

#3 trancexx

trancexx

    Hm, I really shouldn't.

  • Active Members
  • PipPipPipPipPipPip
  • 5,191 posts

Posted 29 June 2012 - 10:58 AM

0xFF0000 is just a number. That's clear right?
"FF0000" is hexadecimal string representation of that number.
Dec() returns a numeric representation of a hexadecimal string.

Therefore, if I want to turn "FF0000" into 0xFF0000 I have to Dec() it.
b(Dec(Hex(BinaryMid(Dec("FF0000"), 1, 3))))

  • CaptainClucks likes this

eMyvnE


#4 CaptainClucks

CaptainClucks

    Unum Cavillator Spuria

  • Active Members
  • PipPipPipPipPipPip
  • 1,216 posts

Posted 29 June 2012 - 11:05 AM

0xFF0000 is just a number. That's clear right?
"FF0000" is hexadecimal string representation of that number.
Dec() returns a numeric representation of a hexadecimal string.

Affirmative.

Here's what I was after.

Func _Rev($Color)     Switch StringIsInt($Color)         Case True             Return Dec(Hex(BinaryMid($Color, 1, 3)))         Case False             Return Dec(Hex(BinaryMid(Dec(StringReplace($Color,"0x","")), 1, 3)))     EndSwitch EndFunc   ;==>_Rev

Edited by ApudAngelorum, 29 June 2012 - 11:12 AM.

Spoiler
Warning: Posts by this user are subject to change or may disappear without notice.

#5 trancexx

trancexx

    Hm, I really shouldn't.

  • Active Members
  • PipPipPipPipPipPip
  • 5,191 posts

Posted 29 June 2012 - 12:48 PM

More correct is to use If...Then than Switch for that. That's typical misuse.
Ternary operator too eventually.

edit:
Here's a question for you. How would you write that function using ternary operator?
(There are several ways, but only one is actually correct).
Don't disappoint me. :D

Edited by trancexx, 29 June 2012 - 12:51 PM.

eMyvnE


#6 CaptainClucks

CaptainClucks

    Unum Cavillator Spuria

  • Active Members
  • PipPipPipPipPipPip
  • 1,216 posts

Posted 29 June 2012 - 02:32 PM

More correct is to use If...Then than Switch for that. That's typical misuse.
Ternary operator too eventually.

edit:
Here's a question for you. How would you write that function using ternary operator?
(There are several ways, but only one is actually correct).
Don't disappoint me. :D

I remember reading a statment you made in the dev chat about enclosing them. So I would probably be cheating.

AutoIt         
#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_AutoIt3=C:Program FilesAutoIt3Betaautoit3.exe #AutoIt3Wrapper_Run_AU3Check=n #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** $a1 = _Rev("090000") $b1 = _Rev(0x090000) ConsoleWrite((Random(0, 1, 1) ? a($a1) : b($b1))&@CR) Sleep(3000) Func a($a2) GUICreate("") GUISetBkColor($a2) GUISetState() Return "a" EndFunc Func b($b2) GUICreate("") GUISetBkColor($b2) GUISetState() Return "b" EndFunc Func _Rev($Color) Return (StringIsInt($Color) ? Dec(Hex(BinaryMid($Color, 1, 3))) : Dec(Hex(BinaryMid(Dec(StringReplace($Color,"0x","")), 1, 3)))) EndFunc ;==>_Rev



Also, there seems to be a problem, that color I'm converting should be a navy blue hue, instead I am getting a black color, why?
Spoiler
Warning: Posts by this user are subject to change or may disappear without notice.

#7 trancexx

trancexx

    Hm, I really shouldn't.

  • Active Members
  • PipPipPipPipPipPip
  • 5,191 posts

Posted 29 June 2012 - 02:51 PM

I remember reading a statment you made in the dev chat about enclosing them. So I would probably be cheating.

AutoIt         
#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_AutoIt3=C:Program FilesAutoIt3Betaautoit3.exe #AutoIt3Wrapper_Run_AU3Check=n #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** $a1 = _Rev("090000") $b1 = _Rev(0x090000) ConsoleWrite((Random(0, 1, 1) ? a($a1) : b($b1))&@CR) Sleep(3000) Func a($a2) GUICreate("") GUISetBkColor($a2) GUISetState() Return "a" EndFunc Func b($b2) GUICreate("") GUISetBkColor($b2) GUISetState() Return "b" EndFunc Func _Rev($Color) Return (StringIsInt($Color) ? Dec(Hex(BinaryMid($Color, 1, 3))) : Dec(Hex(BinaryMid(Dec(StringReplace($Color,"0x","")), 1, 3)))) EndFunc ;==>_Rev

I'm disappointed.

Also, there seems to be a problem, that color I'm converting should be a navy blue hue, instead I am getting a black color, why?

Funny guy.

Edited by trancexx, 29 June 2012 - 02:52 PM.

eMyvnE


#8 AZJIO

AZJIO

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 997 posts

Posted 29 June 2012 - 03:48 PM

it is not black it is dark blue. And the one that bog-yellow - is wrong. The problem is " ( ) ? ( ) : ( ) " If we write the function as standard, it works.
Func _Rev($Color)     If IsString($Color) Then $Color = Dec(StringReplace($Color, "0x", ""))     Return Dec(Hex(BinaryMid($Color, 1, 3))) EndFunc


#9 CaptainClucks

CaptainClucks

    Unum Cavillator Spuria

  • Active Members
  • PipPipPipPipPipPip
  • 1,216 posts

Posted 29 June 2012 - 04:46 PM

I'm disappointed.


Funny guy.

Ok, so I just got back from my laboratory, dissected a goldfish and installed a new brain, and now I am able to see the fallacy in my previous post. Let me throw some context in the whole of this.

I'm working (playing really) with scintilla. I'm making a script that parses the SciTE configuration files and am extracting the color values.
As I have noticed, the color values are stored in RGB format, but they need to be sent to the scintilla control in BGR format.

This code causes the syntax highlighting for functions to appear grey, while everything else is highlighted as expected.
$Function_Fore = _GetProperty($SciteConfig, "style.au3.4","fore"); the _Rev() func is used in this call SetStyle($Sci, $SCE_AU3_FUNCTION, $Function_Fore, $Function_Back, 0, "", 1, 1)

and using this works fine.



$Function_Fore = _Rev(0x000090) SetStyle($Sci, $SCE_AU3_FUNCTION, $Function_Fore, $Function_Back, 0, "", 1, 1)

In the first example, the string is returned as "000090" and then I use the _rev() func to convert it for use with Scintilla and it appears black.

Func _GetProperty($Config, $Property, $Which = 0, $Rev = 0) Local $Test = StringRegExp($Config, StringReplace($Property, ".", ".") & "=(.*)[rn]", 3) If @error Then Return SetError(1, 0, 0x000000) Switch StringLower($Which) Case "fore" ; Foreground color $Test = StringRegExp($Test[0], "fore:#(.*?)[,rn]", 3) Case "back" ; Background color $Test = StringRegExp($Test[0], "back:#(.*?)[,rn]", 3) Case "#" ; just the color $Test = StringRegExp($Test[0], "#(.*?)[,rn]", 3) EndSwitch If $Rev Then $Test[0] = _Rev($Test[0]) EndIf If @error Then Return SetError(1, 0, 0x000000) Return SetError(0, 0, $Test[0]) EndFunc ;==>_GetProperty

Currently I have an unholy amount of free time on my hands and and decided to spend it doing this.

Posted Image

Like I was saying, it seems that some colors are not being converted correctly or something, I'm still trying to figure this out.

$Function_Fore = _Rev('000090');Returns incorrect conversion - 3158064 / looks grey/black $Function_Fore = _Rev('0x000090');Returns correct conversion - 9437184 / looks navy blue $Function_Fore = _Rev(0x000090);Returns correct conversion - 9437184 / looks navy blue $Function_Fore = 0x000090; Makes functions look maroon - 144 $Function_Fore = 0x090000; Makes functions look grey/black - 589824

Edit: I forgot to beg you for insight on correct usage, you always make statements like that and then disappear, leaving people baffled and disoriented.

Edited by ApudAngelorum, 29 June 2012 - 05:19 PM.

Spoiler
Warning: Posts by this user are subject to change or may disappear without notice.

#10 CaptainClucks

CaptainClucks

    Unum Cavillator Spuria

  • Active Members
  • PipPipPipPipPipPip
  • 1,216 posts

Posted 29 June 2012 - 05:29 PM

Ok, thanks everybody, after some good old fashioned humiliation, I've discovered the cause of this anomaly.

The culprit was the way I used trancexxs magical functions.
This seems to work fine.

Func _Rev($Color) If IsString($Color) Then Return Dec(Hex(BinaryMid(Dec(StringReplace($Color,"0x","")), 1, 3))) Return Dec(Hex(BinaryMid($Color, 1, 3))) EndFunc ;==>_Rev


it is not black it is dark blue. And the one that bog-yellow - is wrong. The problem is " ( ) ? ( ) : ( ) " If we write the function as standard, it works.

Func _Rev($Color)     If IsString($Color) Then $Color = Dec(StringReplace($Color, "0x", ""))     Return Dec(Hex(BinaryMid($Color, 1, 3))) EndFunc

Oh god, I don't know how I missed your post, only if I would have seen it earlier...
The IsString thing was what I was needing so badly.

My knowledge in binary and string conversions is limited.

Edited by ApudAngelorum, 29 June 2012 - 05:34 PM.

Spoiler
Warning: Posts by this user are subject to change or may disappear without notice.

#11 trancexx

trancexx

    Hm, I really shouldn't.

  • Active Members
  • PipPipPipPipPipPip
  • 5,191 posts

Posted 29 June 2012 - 09:10 PM

Edit: I forgot to beg you for insight on correct usage, you always make statements like that and then disappear, leaving people baffled and disoriented.

I do? Shit.
Can you point me to some alwayses? I would like to see that for my self.

eMyvnE


#12 JohnOne

JohnOne

    John

  • Active Members
  • PipPipPipPipPipPip
  • 8,857 posts

Posted 29 June 2012 - 09:36 PM

If you're not careful, she'll also make a coat out of your dalmatians.
AutoIt Absolute Beginners Require a serial
Run('hh mk:@MSITStore:'&StringReplace(@AutoItExe,'.exe','.chm')&'::/html/tutorials/helloworld/helloworld.htm','',@SW_MAXIMIZE)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users