Sign in to follow this  
Followers 0
kw_rock

Converting user input colors

7 posts in this topic

Hey guys! I have been doing tons of searching and I have come up with bits and pieces but nothing very useful!

I want something to convert colors for me in AutoIT. Pixel searchs and such either return Hex or Decimal values.. so I want it to convert RGB to decimal.

Basically, I want an input box that pops up with 3 slots, one for red, green and blue.

Then, I want the script to convert the string as a whole into decimal and/or hex.

I've found a lot of functions for different color related things, but none of them for decimal :)

Any help would be appreciated guys!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#include <COLOR.AU3>
_ColorGetRed
_ColorGetBlue
_ColorGetGreen

Just use Int() or Hex() to show values as wanted.

Edited by funkey

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

_ColorGetRGB() returns an array of the values. It's in the help file.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I understand that, i've looked over the help file.

However, i'm still not having luck. Everything is related to converting Hex to things not converting things to hex. I've tired multiple functions and still haven't gotten anywhere CLOSE to my objective.

If _ColorGetRGB() returns an array of the values... I want the user to enter in an RGB value and have the Hex/Dec returned, not convert a Hex to RGB

I found this somewhere, but once again it's the opposite of what I want.

$nColor = 0xAABB0A
$Blue = BitAND($nColor, 0xFF)
$Green = BitAND(BitShift($nColor, 8), 0xFF)
$Red = BitAND(BitShift($nColor, 16), 0xFF)

ConsoleWrite("Red: " & $Red & " Green: " & $Green & " Blue: " & $Blue & @CRLF)
Report

Honestly, all I want is something like:

$RGB = InputBox("RGB to Hex/Dec", "Enter RGB", "")

$RGB = Hex($RGB)

I am not asking for someone to type out the whole code.. just give me a little more than a function to use because I generally don't understand the descriptions they give in the help file :)

Edited by kw_rock

Share this post


Link to post
Share on other sites

Look at _ColorSetRGB, that will convert the 3 color values Red, Green, and Blue to a Decimal number. You can use either Hex representations of the 3 numbers or decimal or any combination of the 2 when you send it to that function.

This script was modified from the example script in the help file for _ColorSetRGB that shows what I mean. If you run it in SciTe you can see the value returned by the function in the console output pane of the editor.

#include <Color.au3>

Dim $aColor[3] = [0x80, 0x90, 255] ; I changed the last value from 0xff to the decimal equivalent to show that it works with both.

$nColor = _ColorSetRGB($aColor)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $nColor = ' & $nColor & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
MsgBox(4096, "AutoIt", " Red=" & Hex($aColor[0], 2) & " Blue=" & Hex($aColor[1], 2) & " Green=" & Hex($aColor[2], 2) & @CRLF & _
        "Color=" & Hex($nColor))
1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Amazing!!

Thank you very much!

Share this post


Link to post
Share on other sites

Here are another three methods of converting the three colour values to a RGB colour.

Note the ease of of identifying RGB colours in 0xRRGGBB hexadecimal format.

e.g. 0x0000FF if blue (blue color channel is full); 0xFF0000 is red (red color channel is full); 0x000000 if black (no color in any of the color channels); 0xFFFFFF is white (all color channels are full).

Local $red = 0x80
Local $green = 0x90
Local $blue = 255

ConsoleWrite("The three color channels are:-" & @LF)
ConsoleWrite("red   = 0x" & Hex($red, 2) & " or " & $red & "; " & @LF)
ConsoleWrite("green = 0x" & Hex($green, 2) & " or " & $green & "; " & @LF)
ConsoleWrite("blue  = 0x" & Hex($blue, 2) & " or " & $blue & "; " & @LF)

;---  Using String to number - color channels to color --------------
Local $color = Execute("0x" & Hex($red, 2) & Hex($green, 2) & Hex($blue, 2)) ; Use either Execute(), Int(), or Number() function.
ConsoleWrite("RGB Color Execute String" & @TAB & "0x" & Hex($color, 6) & " or " & $color & @LF)

;--- Multiply & Add color channels together to get color ------------
Local $colorAddMulti = ($red * 0x10000 + $green * 0x100 + $blue)
ConsoleWrite("RGB Color Add/Multiply  " & @TAB & "0x" & Hex($colorAddMulti, 6) & " or " & $colorAddMulti & @LF)

; --- Using Bit operators - color channels to color ----------------
Local $colorBitWise = BitOR(BitShift(($red), -16), BitShift(($green), -8), ($blue))
ConsoleWrite("RGB Color Bit operations" & @TAB & "0x" & Hex($colorBitWise, 6) & " or " & $colorBitWise & @LF)

#cs
    Output:-
    The three color channels are:-
    red   = 0x80 or 128;
    green = 0x90 or 144;
    blue  = 0xFF or 255;
    RGB Color Execute String    0x8090FF or 8425727
    RGB Color Add/Multiply      0x8090FF or 8425727
    RGB Color Bit operations    0x8090FF or 8425727
#ce
1 person likes this

Share this post


Link to post
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
Sign in to follow this  
Followers 0