Easy Question about Colors Topic is solved

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

Easy Question about Colors

11 Aug 2017, 14:22

I have 2 color

Color1 = 0xFF040D4D
Image

Color2 = 0xFF4D040D
Image

How mix it and get this?
Image

ColorResult = 0xFF310825

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

Re: Easy Question about Colors

11 Aug 2017, 15:35

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.

Code: [Select all] [Download] GeSHi © Codebox Plus

SetFormat, Integer, H
var1:=0x4D
var2:=0x04
average:=(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 % average
return



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.)

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



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.
User avatar
jeeswg
Posts: 1670
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Easy Question about Colors  Topic is solved

11 Aug 2017, 15:58

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

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



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.
User avatar
masheen
Posts: 200
Joined: 06 Dec 2016, 14:10

Re: Easy Question about Colors

12 Aug 2017, 03:08

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:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



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
User avatar
jeeswg
Posts: 1670
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Easy Question about Colors

12 Aug 2017, 07:02

This should do it:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by jeeswg on 13 Aug 2017, 07:23, edited 1 time in total.
User avatar
masheen
Posts: 200
Joined: 06 Dec 2016, 14:10

Re: Easy Question about Colors

13 Aug 2017, 00:36

jeeswg wrote:This should do it:

Code: [Select all] [Download] GeSHi © Codebox Plus

q:: ;check if R/G/B values are equal to each other
vCol := 0xFF5D5D5D
;vCol := 0xFF5D5D5E

if ((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: 4415
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Easy Question about Colors

13 Aug 2017, 05:49

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.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


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

Re: Easy Question about Colors

13 Aug 2017, 07:27

@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.

Return to “Ask For Help”

Who is online

Users browsing this forum: Elgin, jassod, newbieforever, The Old Lie, Trigg and 20 guests