## Easy Question about Colors Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

I have 2 color

Color1 = 0xFF040D4D

Color2 = 0xFF4D040D

How mix it and get this?

ColorResult = 0xFF310825

i need
Color1 - Color2
or Color1 / Color2
or what plz tell me.
Posts: 2863
Joined: 17 Oct 2015, 20:28

### Re: Easy Question about Colors

Isolate each color (>>operator I think can do this for ya, but I haven't done it that way myself -- it'll be able to shorten up the math though to get it all done in one go -- you also need to do something with the & operator and 0xFF... it's on the forums somewhere).

If you have the values 4D and 04, and if you use SetFormat to do Hexadecimal for your maths. It looks like you want the average, but that doesn't quite work right.

`SetFormat, Integer, Hvar1:=0x4Dvar2:=0x04average:=(var1+var2)//2 ; floor divide, otherwise you get an odd number which when divided by 2 becomes a fraction and appears as a decimal.MsgBox % averagereturn`

Edit:
All this math can be compressed into four lines (or even one if you wanted), and then the concatenation for the final_answer. Probably could do well with a function that takes the inputs and splits into an array or better name variables to do a loop through them. But, whichever. Just a proof of concept.

And jeeswg is right below, we shouldn't use floor divide, because that forces a round down. So Round() is better. The code below reflects that. (Though when it was doing round down, `g_avg:=(g1+g2)//2` gave the answer of 0x8 (0x08) which matches up with the third byte in your expected result, OP.)

But that only comes up with a final answer of 0xFF29092D, which is off from the desired 0xFF310825 - assuming these are RGB values, the desired value is biased toward red (+0x08) and against blue (-0x08).
Last edited by Exaskryz on 11 Aug 2017, 16:14, edited 1 time in total.
jeeswg
Posts: 4578
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: Easy Question about Colors  Topic is solved

I've been working recently, with a lot of this type of maths. I've found it quite hard to get this concise:

I was tempted to use bitshift, but bitshift rounds down, and any halves when rounded to the nearest integer, should be rounded up.

I also discovered that if you apply the Format function, specifying an integer as the output format, that truncates the integer, instead of rounding it, so I had to use the Round function.

However, it may be possible to improve on my code.

There may be some other algorithm that is calculating your colour 'average', if anyone is familiar with colour averages.

@masheen: If you can mention the program you are using, or provide a link to a website that gives you the same results, that would be useful.
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

### Re: Easy Question about Colors

jeeswg wrote:I've been working recently, with a lot of this type of maths. I've found it quite hard to get this concise:

I was tempted to use bitshift, but bitshift rounds down, and any halves when rounded to the nearest integer, should be rounded up.

I also discovered that if you apply the Format function, specifying an integer as the output format, that truncates the integer, instead of rounding it, so I had to use the Round function.

However, it may be possible to improve on my code.

There may be some other algorithm that is calculating your colour 'average', if anyone is familiar with colour averages.

@masheen: If you can mention the program you are using, or provide a link to a website that gives you the same results, that would be useful.

thx. Can u tell me how i can to know grayscale color or not?
example i have color 0xFF5D5D5D
how to do?
if R = G = B = Grayscale
jeeswg
Posts: 4578
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: Easy Question about Colors

This should do it:
Last edited by jeeswg on 13 Aug 2017, 07:23, edited 1 time in total.
masheen
Posts: 295
Joined: 06 Dec 2016, 14:10

### Re: Easy Question about Colors

jeeswg wrote:This should do it:
`q:: ;check if R/G/B values are equal to each othervCol := 0xFF5D5D5D;vCol := 0xFF5D5D5Eif ((vCol>>16)&0xFF = vCol&0xFF) && ((vCol>>8)&0xFF = vCol&0xFF) MsgBox, % "y"else MsgBox, % "n"return`

thx work goood.
Last question if i have color without alpha like this 0x5D5D5D
can u help?
just me
Posts: 5421
Joined: 02 Oct 2013, 08:51
Location: Germany

### Re: Easy Question about Colors

I found two interesting articles:
Both don't retrieve the result you posted in the OP (0x310825). The first one is a fast replacement for the methods posted by Exaskryz and jeeswg but is rounding down by design.

You have the agony of choice!
jeeswg
Posts: 4578
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: Easy Question about Colors

@masheen: I wrote it in such a way that it handles RGB values with/without an alpha value. If you look over the code you will see that it doesn't matter whether the value has a alpha value or not.