Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Calculate the opposite of color!

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Fri Jul 30, 2021 12:32 pm    Post subject: Calculate the opposite of color! Reply with quote

The title can be a little complicated.
I will ask with code below.
When a panel color is dark, the panel text must be light,
or when the panel color is light, the panel text should be dark.
How to calculate this, I am puzzled.
here is the code i use:

Code:
if form then form.destroy() form = nil end
form = createForm()
form.Position=poDesktopCenter

Pnl2=createPanel(form)
Pnl2.Caption="+" Pnl2.Font.Size=40

local clr1=65407 --16776960  --16711680 --
Pnl2.Color=clr1

if Pnl2.Color > 0xFFF000 or Pnl2.Color > 8388607 then
Pnl2.Font.Color=16777215
else
Pnl2.Font.Color=0
end
Back to top
View user's profile Send private message
DanyDollaro
Master Cheater
Reputation: 3

Joined: 01 Aug 2019
Posts: 334

PostPosted: Fri Jul 30, 2021 1:15 pm    Post subject: Reply with quote

To get the complementar color you can just use the "complementar to one" operator:
Code:
clr = 0x00FF00FF
print(~clr)

The output will be -16711936, in hexadecimal 0xFF00FF00.

EDIT: I did some tests with the lua engine, and I noticed that using that operator the color will not be applied as the most significant bytes will be set to 0xFF making the color invalid (i think), and given the absence of the bitwise operators you should do something like this:
Code:
clr = 0xFFFFFF
print(~clr - 0xFFFFFFFFFF000000)

In this case the output will be 0.
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1053
Location: 0x90

PostPosted: Fri Jul 30, 2021 3:37 pm    Post subject: Reply with quote

Here is a little something to invert RGB

Code:

function invertRGB(r,g,b)
r = r * -1 + 255
g = g * -1 + 255
b = b * -1 + 255
print('Hex : ' .. string.format('0x%X%X%X',r,g,b))
print('RGB : ' ..b..','..g..','..r)
return r,g,b
end

invertRGB(100,255,25)


I hope this puts you in the right direction.


Last edited by LeFiXER on Fri Jul 30, 2021 5:49 pm; edited 1 time in total
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Fri Jul 30, 2021 4:42 pm    Post subject: Reply with quote

I have a color code that I am currently using.
It can be used straight or reversed.

Code:
function BGRToHex(blue, green, red)
 if(red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255) then
 return nil
 end
 return string.format("0x%.2X%.2X%.2X", blue,green,red)
end
---------------------------------------------------
clr=16477777

 b,g,r = clr >> 16 & 0xff, clr >> 8 & 0xff, clr & 0xff
 col = BGRToHex(b,g,r)
print("Color: "..tostring(col))

 col1 = BGRToHex(r,g,b)
print("Color1: "..tostring(col1))


As I wrote in the statement; Don't get hung up on the title, the question is in the description!

Code:
if form then form.destroy() form = nil end
form = createForm()
form.Position=poDesktopCenter

Pnl2=createPanel(form)
Pnl2.Caption="+" Pnl2.Font.Size=40

local clr1=65407 --16776960  --16711680 --
Pnl2.Color=clr1

if Pnl2.Color > 0xFFF000 or Pnl2.Color > 8388607 then
Pnl2.Font.Color=16777215
else
Pnl2.Font.Color=0
end


3 color samples are given above.

Question: If the panel color is dark, the Panel description (Font) must be light.
Or, if the panel color is light, the panel description should be dark.
How can I provide this color contrast?

Sample; In this color font "16776960" should be dark (black), but the code makes the font white.


EDIT: Extended.
Note the yellow box. The "+" is too dark to be visible.
The test code I created is incomplete.
How can I provide all the opposite?

Code:
if form then form.destroy() form = nil end
form = createForm()
form.Position=poDesktopCenter

Pnl2=createPanel(form)
Pnl2.Caption="+" Pnl2.Font.Size=40

Pnl1=createPanel(form)
Pnl1.Caption="+" Pnl1.Font.Size=40 Pnl1.Top=60

Pnl3=createPanel(form)
Pnl3.Caption="+" Pnl3.Font.Size=40 Pnl3.Top=120

Pnl4=createPanel(form)
Pnl4.Caption="+" Pnl4.Font.Size=40 Pnl4.Top=180

Pnl2.Color=0xFFFFFF
Pnl1.Color=0xFFFF00
Pnl3.Color=0x00FFFF
Pnl4.Color=0x000000

if Pnl2.Color < 0xFFF000 or Pnl2.Color < 8388607 then
Pnl2.Font.Color=16777215
else
Pnl2.Font.Color=0
end

if Pnl1.Color < 0xFFF000 or Pnl1.Color < 8388607 then
Pnl1.Font.Color=16777215
else
Pnl1.Font.Color=0
end

if Pnl3.Color < 0xFFF000 or Pnl3.Color < 8388607 then
Pnl3.Font.Color=16777215
else
Pnl3.Font.Color=0
end

if Pnl4.Color < 0xFFF000 or Pnl4.Color < 8388607 then
Pnl4.Font.Color=16777215
else
Pnl4.Font.Color=0
end
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1053
Location: 0x90

PostPosted: Fri Jul 30, 2021 5:39 pm    Post subject: Reply with quote

LeFiXER wrote:
Here is a little something to invert RGB

Code:

function invertRGB(r,g,b)
r = r * -1 + 255
g = g * -1 + 255
b = b * -1 + 255
print('Hex : ' .. string.format('0x%X%X%X',r,g,b))
print('RGB : ' ..b..','..g..','..r)
return r,g,b
end

invertRGB(100,255,25)


I hope this puts you in the right direction.


I think you misunderstand the purpose of this code. It inverts the colour and returns a hex code value which is what CE prefers when using Lua.

Format: 0xXXXXXX



Quote:

Sample; In this color font "16776960" should be dark (black), but the code makes the font white.


That's because "16776960" isn't a colour code. Use my function to convert RGB to the correct colours that you need.
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Fri Jul 30, 2021 7:24 pm    Post subject: Reply with quote

I don't need an inverse of the color.
I'm just trying to do this;
If the panel color is dark; Panel font color should be light color.
If the panel color is light; Panel font color should be dark.

Check out the code below;
In the yellow panel, the font color changed to light color.
Other panel colors responded correctly.

Code:
if form then form.destroy() form = nil end
form = createForm()
form.Position=poDesktopCenter

Pnl2=createPanel(form)
Pnl2.Caption="+" Pnl2.Font.Size=40

Pnl1=createPanel(form)
Pnl1.Caption="+" Pnl1.Font.Size=40 Pnl1.Top=60

Pnl3=createPanel(form)
Pnl3.Caption="+" Pnl3.Font.Size=40 Pnl3.Top=120

Pnl4=createPanel(form)
Pnl4.Caption="+" Pnl4.Font.Size=40 Pnl4.Top=180

Pnl2.Color=0xFFFFFF
Pnl1.Color=0xFFFF00
Pnl3.Color=0x00FFFF
Pnl4.Color=0x000000

if Pnl2.Color < 0xFFF000 or Pnl2.Color < 8388607 then
Pnl2.Font.Color=16777215
else
Pnl2.Font.Color=0
end

if Pnl1.Color < 0xFFF000 or Pnl1.Color < 8388607 then
Pnl1.Font.Color=16777215
else
Pnl1.Font.Color=0
end

if Pnl3.Color < 0xFFF000 or Pnl3.Color < 8388607 then
Pnl3.Font.Color=16777215
else
Pnl3.Font.Color=0
end

if Pnl4.Color < 0xFFF000 or Pnl4.Color < 8388607 then
Pnl4.Font.Color=16777215
else
Pnl4.Font.Color=0
end
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

Joined: 09 May 2003
Posts: 25262
Location: The netherlands

PostPosted: Sat Jul 31, 2021 1:28 am    Post subject: Reply with quote

newcolor=oldcolor ^ 0xffffff

i find mask 0x555555 giving a nice result most of the time

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Sat Jul 31, 2021 6:28 am    Post subject: Reply with quote

Dark Byte wrote:
newcolor=oldcolor ^ 0xffffff

i find mask 0x555555 giving a nice result most of the time


Thanks DB, that "^" sign led me to Lua math.
The code below will generate a random color close to the opposite color.

Even if the panel color changes, the panel text will be legible.

PS: Thanks DB for understanding what I mean.

Code:
function newRandomClr(rsclr1,rsclr2)
rstClr1=math.random(math.min(rsclr1,rsclr2),math.max(rsclr1,rsclr2))
--print(rstClr1)
return rstClr1
end

Pnl2.Font.Color=newRandomClr(Pnl2.Font.Color,Pnl2.Color)

Pnl1.Font.Color=newRandomClr(Pnl1.Color,Pnl1.Font.Color)

Pnl3.Font.Color=newRandomClr(Pnl3.Color,Pnl3.Font.Color)

Pnl4.Font.Color=newRandomClr(Pnl4.Color,Pnl4.Font.Color)
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1053
Location: 0x90

PostPosted: Sat Jul 31, 2021 10:44 am    Post subject: Reply with quote

ByTransient wrote:
I don't need an inverse of the color.
I'm just trying to do this;
If the panel color is dark; Panel font color should be light color.
If the panel color is light; Panel font color should be dark.


That Is exactly what you're asking. You want a light colour based on the colour of the panel therfore you can take the panel colour and inverse it based on whether it's light or dark.
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Sat Jul 31, 2021 11:55 am    Post subject: Reply with quote

It's a bit complicated, but it works. Wink

Code:
function BGRToHex11(item1)
clr=item1.color

 blue,green, red = clr >> 16 & 0xff, clr >> 8 & 0xff, clr & 0xff

 if(red < 0 or red > 255 or green < 0 or green > 255 or blue < 0 or blue > 255) then
 return nil
 end
--print("blue: "..blue.."\ngreen: "..green.."\nred: "..red)

if blue==0 then blue=510 end
if blue==255 then blue=0 end
blue1=tonumber(blue) / 2
blue1=math.floor(blue1)

if green==0 then green=510 end
if green==255 then green=0 end
green1=tonumber(green) / 2
green1=math.floor(green1)

if red==0 then red=510 end
if red==255 then red=0 end
red1=tonumber(red) / 2
red1=math.floor(red1)

--print("blue1: "..blue.."\ngreen1: "..green.."\nred1: "..red)
 return string.format("0x%.2X%.2X%.2X", blue1,green1,red1)
end
---------------------------------------------------
if form then form.destroy() form = nil end
form = createForm()
form.Position=poDesktopCenter

Pnl2=createPanel(form)
Pnl2.Caption="+" Pnl2.Font.Size=40

Pnl1=createPanel(form)
Pnl1.Caption="+" Pnl1.Font.Size=40 Pnl1.Top=60

Pnl3=createPanel(form)
Pnl3.Caption="+" Pnl3.Font.Size=40 Pnl3.Top=120

Pnl4=createPanel(form)
Pnl4.Caption="+" Pnl4.Font.Size=40 Pnl4.Top=180

Pnl2.Color=0xFFFFFF --0xFF00FF
Pnl1.Color=0xFFFF00
Pnl3.Color=0x00FFFF
Pnl4.Color=0x000000 --0x00FF00

Pnl2.Font.Color=BGRToHex11(Pnl2)

Pnl1.Font.Color=BGRToHex11(Pnl1)

Pnl3.Font.Color=BGRToHex11(Pnl3)

Pnl4.Font.Color=BGRToHex11(Pnl4)



Ek1.PNG
 Description:
 Filesize:  21.55 KB
 Viewed:  1553 Time(s)

Ek1.PNG


Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites