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 


Color Lerp In Lua

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Wed Dec 28, 2022 10:05 pm    Post subject: Color Lerp In Lua Reply with quote

How to write this to Lua CE?.

Code:
function ColorLerp(col1, col2 : TColor; factor : single {0.0 .. 1.0}) : TColor;

  function ByteLerp(val1, val2 : byte; factor : single) : byte;
  begin
    Result := Round( (val1 * factor) + ( (1 - factor) * val2) );
  end;

begin
  Result := RGBToColor
  (
      ByteLerp(Red(col1), Red(col2), factor ),
      ByteLerp(Green(col1), Green(col2), factor ),
      ByteLerp(Blue(col1), Blue(col2), factor )
  );
end;

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Wed Dec 28, 2022 11:28 pm    Post subject: Reply with quote

Code:
function ByteLerp(val1, val2, factor)
  return math.floor(val1 * factor +  val2 * (1 - factor))
end

function ColorLerp(col1, col2, factor)
  return {
    r = ByteLerp(col1.r, col2.r, factor),
    g = ByteLerp(col1.g, col2.g, factor),
    b = ByteLerp(col1.b, col2.b, factor),
  }
end

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Wed Dec 28, 2022 11:53 pm    Post subject: This post has 1 review(s) Reply with quote

Thanks @ParkourPenguin for reply. I try your script, but return error "attempt to index a number value (local 'col1')". I think because i represent color in hex format.

But this one just work fine even get flickering when change the track bar.

Code:
function createShape(Parent)
 local sh = createComponentClass('TShape', Parent)
 sh.Parent = Parent
 return sh
end

if f then f.destroy() end

f = createForm()
f.Left = 318
f.Height = 270
f.Top = 247
f.Width = 520
f.Caption = 'Custom Progress Bar With Color Lerp'

TrackBar1 = createTrackBar(f)
TrackBar1.Left = 42
TrackBar1.Height = 46
TrackBar1.Top = 34
TrackBar1.Width = 338
TrackBar1.Max = 100
TrackBar1.Position = 50

Shape1 = createShape(f)
Shape1.Left = 42
Shape1.Height = 49
Shape1.Top = 127
Shape1.Width = 425


ColorLow = 0x0000FF  --clRed
ColorHigh = 0x00FF00 --clGreen
InitialSize = Shape1.Width

function clamp(number) return number end
function lerp(a,b,t) return math.floor(a+(b-a)*t) end
function lerpRGB(r,g,b,r2,g2,b2, t) return lerp(r,r2,t), lerp(g,g2,t), lerp(b,b2,t) end
function lerpColor(r,g,b) return r | (g<<8) | (b<<16) end

function TrackBar1Change()
 local factor
 factor = TrackBar1.Position / TrackBar1.Max
 Shape1.Brush.Color = lerpColor(lerpRGB(ColorLow,0,0, ColorHigh,0,0, factor))
 Shape1.Width = math.floor(InitialSize * factor)
end

f.show()
TrackBar1.OnChange = TrackBar1Change

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Thu Dec 29, 2022 1:05 am    Post subject: Reply with quote

If you read my code, you'll see the parameters are tables with keys "r", "g", and "b". You didn't specify what the parameters should be.

IIRC the color property is typically some integer value where the first 3 bytes are R/G/B values and the remaining bytes comprise some bitfield that stores properties of the color.

It seems that original pascal code has col1 / col2 backwards compared to typical lerp interfaces.

The arguments you pass to lerpRBG are wrong. (decompose ColorLow / ColorHigh into bytes)

Code:
function createShape(Parent)
 local sh = createComponentClass('TShape', Parent)
 sh.Parent = Parent
 return sh
end

if f then f.destroy() end

f = createForm()
f.Left = 318
f.Height = 270
f.Top = 247
f.Width = 520
f.Caption = 'Custom Progress Bar With Color Lerp'

TrackBar1 = createTrackBar(f)
TrackBar1.Left = 42
TrackBar1.Height = 46
TrackBar1.Top = 34
TrackBar1.Width = 338
TrackBar1.Max = 100
TrackBar1.Position = 50

Shape1 = createShape(f)
Shape1.Left = 42
Shape1.Height = 49
Shape1.Top = 127
Shape1.Width = 425


function rgbToColor(r, g, b)
  return r | g << 8 | b << 16
end
function colorToRGB(color)
  return color & 0xFF, color >> 8 & 0xFF, color >> 16 & 0xFF
end
function mergeColorMetadata(meta, color)
  return meta & (~0xFFFFFF) | color & 0xFFFFFF
end

function lerpNumber(num1, num2, factor)
  return math.floor((1 - factor) * num1 + factor * num2)
end
function lerpColor(color1, color2, factor)
  local r1, g1, b1 = colorToRGB(color1)
  local r2, g2, b2 = colorToRGB(color2)

  local r3 = lerpNumber(r1, r2, factor)
  local g3 = lerpNumber(g1, g2, factor)
  local b3 = lerpNumber(b1, b2, factor)

  return rgbToColor(r3, g3, b3)
end

ColorLow = rgbToColor(0xFF, 0, 0)
ColorHigh = rgbToColor(0, 0xFF, 0)

function TrackBar1Change()
 local factor
 factor = TrackBar1.Position / TrackBar1.Max
 local newColor = lerpColor(ColorLow, ColorHigh, factor)
 Shape1.Brush.Color = mergeColorMetadata(Shape1.Brush.Color, newColor)
end
TrackBar1Change()
f.show()
TrackBar1.OnChange = TrackBar1Change

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Thu Dec 29, 2022 2:09 am    Post subject: Reply with quote

I have read your script @ParkourPenguin and see the col1, col2 place on the table under the function. But your next script make me clear understand to separates the rgb value from the color code. Thank you.

Just a little modified on your script to make the shape width increase like a progress bar.

Code:
ColorLow = rgbToColor(0xFF, 0, 0)
ColorHigh = rgbToColor(0, 0xFF, 0)
InitialSize = Shape1.Width

function TrackBar1Change()
 local factor
 factor = TrackBar1.Position / TrackBar1.Max
 newColor = lerpColor(ColorLow, ColorHigh, factor)
 Shape1.Brush.Color = mergeColorMetadata(Shape1.Brush.Color, newColor)
 Shape1.Width = math.floor(InitialSize * factor)
end


And this can implemented in a game, for example to control HP, MP, and others by get the address of the player properties.

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 31

Joined: 16 Feb 2017
Posts: 1234

PostPosted: Sat Dec 31, 2022 2:10 pm    Post subject: Reply with quote

Looks nice to play with in codes. Smile
Thanks.


Code:
function createShape(Parent)
 local sh = createComponentClass('TShape', Parent)
 sh.Parent = Parent
 return sh
end

if f1 then f1.destroy() end

f1 = createForm()
f1.Left = 318
f1.Height = 580
f1.Top = 247
f1.Width = 570
f1.Caption = 'Custom Progress Bar With Color Lerp'

TrackBar1 = createTrackBar(f1)
TrackBar1.Left = 60
TrackBar1.Height = 25
TrackBar1.Top = 10
TrackBar1.Width = 455
TrackBar1.Max = 1000
TrackBar1.Position = 0

Shape1 = createShape(f1)
Shape1.Left = 60
Shape1.Height = 49
Shape1.Top = 80
Shape1.Width = 455


ColorLow = 0x0000FF  --clRed --255
ColorHigh = 255255255  --16711680
InitialSize = Shape1.Width

local aa1 = 5
local aa2 = 5
local aa3 = 0
local aa4 = 1
local page = 1
local pst = 0
local pngTbl = {}

function clamp(number) return number end
function lerp(a,b,t) return math.floor(a+(b-a)*t) end
function lerpRGB(r,g,b,r2,g2,b2, t) return lerp(r,r2,t), lerp(g,g2,t), lerp(b,b2,t) end
function lerpColor(r,g,b) return r | (g<<8) | (b<<16) end

pngTbl.Lbl1 = createLabel(f1)
pngTbl.Lbl1.AutoSize = false
pngTbl.Lbl1.Height = 30
pngTbl.Lbl1.Width = 550 --255255255 --2808060
pngTbl.Lbl1.Left = 10
pngTbl.Lbl1.Top = 50
pngTbl.Lbl1.Alignment = "taCenter"
pngTbl.Lbl1.OptimalFill = true
pngTbl.Lbl1.Font.Style = "fsBold"
pngTbl.Lbl1.OnClick = function()
 textt = Shape1.Brush.Color
 writeToClipboard(textt)
 end

function TrackBar1Change()
 local factor
 factor = TrackBar1.Position / TrackBar1.Max
 Shape1.Brush.Color = lerpColor(lerpRGB(ColorLow,0,0, ColorHigh,0,0, factor))
 --Shape1.Width = math.floor(InitialSize * factor)
 pngTbl.Lbl1.Caption = "Page: "..page.." - Color: "..Shape1.Brush.Color
 return Shape1.Brush.Color
end

TrackBar1Change()
TrackBar1.OnChange = TrackBar1Change

    pngTbl["pano"..aa4] = createPanel(f1)
    pngTbl["pano"..aa4].Height = 400
    pngTbl["pano"..aa4].Width = 560
    pngTbl["pano"..aa4].Left = 5
    pngTbl["pano"..aa4].Top = 140
    pngTbl["pano"..aa4].caption = pngTbl["pano"..aa4].name
    pngTbl["pano"..aa4].visible = false

for i=1, 1000 do
   if aa3 == 100 then
    aa4=tonumber(aa4)+1
    pngTbl["pano"..aa4] = createPanel(f1)
    pngTbl["pano"..aa4].Height = 400
    pngTbl["pano"..aa4].Width = 560
    pngTbl["pano"..aa4].Left = 5
    pngTbl["pano"..aa4].Top = 140
    pngTbl["pano"..aa4].caption = pngTbl["pano"..aa4].name
    pngTbl["pano"..aa4].visible = false
    aa1 = 5
    aa2 = 5
    aa3 = 0
   end
  TrackBar1.Position = i
  pngTbl["pnl"..i] = createPanel(pngTbl["pano"..aa4])
  pngTbl["pnl"..i].Height = 30
  pngTbl["pnl"..i].Width = 50
  pngTbl["pnl"..i].Left = tonumber(aa1)
  pngTbl["pnl"..i].Top = tonumber(aa2)
  aa1 = tonumber(aa1) + 55
  if aa1>550 then aa1=5 aa2 = tonumber(aa2) + 40 end
  pngTbl["pnl"..i].color = Shape1.Brush.Color
  pngTbl["pnl"..i].caption = pngTbl["pnl"..i].color
  pngTbl["pnl"..i].OnClick=function() --print(pngTbl["pnl"..i].color)
  Shape1.Brush.Color = pngTbl["pnl"..i].color
  pst = i
  TrackBar1.Position = tonumber(pst)
  pngTbl.Lbl1.Caption = "Page: "..page.." - Position: "..pst.." - Color: "..Shape1.Brush.Color
  end
  pngTbl.Lbl1.Caption = "Page: "..page.." - Position: "..i.." - Color: "..Shape1.Brush.Color
  aa3 = tonumber(aa3) + 1
end

pnlL1 = createPanel(f1)
pnlL1.Left = 10
pnlL1.Height = 49
pnlL1.Top = 80
pnlL1.Width = 50
pnlL1.Caption = "<<"
pnlL1.Font.Size = 35

pnlL2 = createPanel(f1)
pnlL2.Left = 515
pnlL2.Height = 49
pnlL2.Top = 80
pnlL2.Width = 50
pnlL2.Caption = ">>"
pnlL2.Font.Size = 35

function allPageFalse()
 for i=1, 10 do
  pngTbl["pano"..i].visible = false
 end
end


pnlL1.OnClick=function()
 allPageFalse()
 if page==1 then
  pngTbl["pano"..page].visible = true
 else
  page = tonumber(page) - 1
  pngTbl["pano"..page].visible = true
 end
  pngTbl.Lbl1.Caption = "Page: "..page.." - Position: "..pst.." - Color: "..Shape1.Brush.Color
end

pnlL2.OnClick=function()
 allPageFalse()
 if page==10 then
  pngTbl["pano"..page].visible = true
 else
  page = tonumber(page) + 1
  pngTbl["pano"..page].visible = true
 end
  pngTbl.Lbl1.Caption = "Page: "..page.." - Position: "..pst.." - Color: "..Shape1.Brush.Color
end

pngTbl.pano1.visible=true
f1.show()



clr1.PNG
 Description:
 Filesize:  56.64 KB
 Viewed:  1409 Time(s)

clr1.PNG



_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Sun Jan 01, 2023 12:03 am    Post subject: Reply with quote

Quote:
Looks nice to play with in codes. Smile
Thanks.


Nice job..!!

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
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