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 


Gradient Text

 
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: Sun Sep 13, 2020 6:57 am    Post subject: Gradient Text Reply with quote

Below is functions to generate gradient text in Lua terminal, adopted from Egor S scripts.

When try in CE Lua, function generate_gradient and print_with_color give result:

Code:
@x021G@x020r@x020a@x055d@x055i@x054e@x090n@x089t@x125 @x125T@x160e@x160x@x196t


How to use function generate_gradient on CE Form?

I tried:

Code:
local abs, min, max, floor = math.abs, math.min, math.max, math.floor
local levels = {[0] = 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff}

local function index_0_5(value) -- value = color component 0..255
   return floor(max((value - 35) / 40, value / 58))
end

local function nearest_16_231(r, g, b)   -- r, g, b = 0..255
   -- returns color_index_from_16_to_231, appr_r, appr_g, appr_b
   r, g, b = index_0_5(r), index_0_5(g), index_0_5(b)
   return 16 + 36 * r + 6 * g + b, levels[r], levels[g], levels[b]
end

local function nearest_232_255(r, g, b)  -- r, g, b = 0..255
   local gray = (3 * r + 10 * g + b) / 14
   -- this is a rational approximation for well-known formula
   -- gray = 0.2126 * r + 0.7152 * g + 0.0722 * b
   local index = min(23, max(0, floor((gray - 3) / 10)))
   gray = 8 + index * 10
   return 232 + index, gray, gray, gray
end

local function color_distance(r1, g1, b1, r2, g2, b2)
   return abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2)
end

local function nearest_term256_color_index(r, g, b)   -- r, g, b = 0..255
   local idx1, r1, g1, b1 = nearest_16_231(r, g, b)
   local idx2, r2, g2, b2 = nearest_232_255(r, g, b)
   local dist1 = color_distance(r, g, b, r1, g1, b1)
   local dist2 = color_distance(r, g, b, r2, g2, b2)
   return dist1 < dist2 and idx1 or idx2
end

local unpack, tonumber = table.unpack or unpack, tonumber

local function convert_color_to_table(rrggbb)
   if type(rrggbb) == "string" then
      local r, g, b = rrggbb:match"(%x%x)(%x%x)(%x%x)"
      return {tonumber(r, 16), tonumber(g, 16), tonumber(b, 16)}
   else
      return rrggbb
   end
end

local function round(x)
   return floor(x + 0.5)
end

local function generate_gradient(text, first_color, last_color)
   local r, g, b = unpack(convert_color_to_table(first_color))
   local dr, dg, db = unpack(convert_color_to_table(last_color))
   local char_pattern = "[^\128-\191][\128-\191]*"
   local n = max(1, select(2, text:gsub(char_pattern, "")) - 1)
   dr, dg, db = (dr - r)/n, (dg - g)/n, (db - b)/n
   local result = ""
   for c in text:gmatch(char_pattern) do
      result = result..("@x%03d"):format(nearest_term256_color_index(
         round(r), round(g), round(b)))..c
      r, g, b = r + dr, g + dg, b + db
   end
   return result
end

---- run on Lua terminal
local function print_with_colors(str)
   print(
      str:gsub("@x(%d%d%d)",
         function(color_idx)
            return "\27[38;5;"..color_idx.."m"
         end)
      .."\27[0m"
   )
end
--------------------------------------------------------------------------------

f=createForm()
pb=createPaintBox(f)
pb.Canvas.Font.Size=20
pb.Align='alClient'

local rect={}
rect.Left=0
rect.Top=0
rect.Right=pb.Width
rect.Bottom=pb.Height

local str = "Gradient Text"
local blue, red = {0, 0, 255}, "#FF0000"
local str = generate_gradient(str, blue, red)  -- gradient from blue to red\
print(str)
--print_with_colors(str)
pb.Canvas.clear()
pb.Canvas.textRect(rect,0,0,str)


The result is nothing text draws on the paint box.
Or is any other CE Lua method to generating gradient text?

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
.lua
Expert Cheater
Reputation: 1

Joined: 13 Sep 2018
Posts: 189

PostPosted: Mon Sep 14, 2020 12:45 am    Post subject: Reply with quote

Code:
pb.Canvas.clear()
Revised as:
Code:
pb.Repaint()
Maybe the result is not what you want:(Refer to the post of dark byte, I don't know where it is)
Code:
function setTextRGB(s)
  local i=1
  local r=''
  for p,cnr in utf8.codes(s) do
    local c=utf8.char(cnr)
    local red=math.random(0,255)  local green=math.random(0,127)  local blue=math.random(0,55)
    c=string.char(27)..'[38;2;'..red..';'..green..';'..blue..'m'..c    r=r..c
  end
  return r
end
f=createForm()
pb=createPaintBox(f)
pb.Canvas.Font.Size=20
pb.Align='alClient'

local rect={}
rect.Left=0
rect.Top=0
rect.Right=pb.Width
rect.Bottom=pb.Height

local str = "Gradient Text"
pb.Repaint()
pb.Canvas.textRect(rect,0,0,setTextRGB(str))



Text.png
 Description:
 Filesize:  40.28 KB
 Viewed:  2164 Time(s)

Text.png


Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Mon Sep 14, 2020 3:06 am    Post subject: Reply with quote

@Lua, thanks for the info. Yes, I knew and I have DB script to generate Rainbow Text as your example. But what I want to do is like attached image.

EDIT:
From the script on the first topic above, here is how to use:

Code:
local function print_with_colors(str)
   print(
      str:gsub("@x(%d%d%d)",
         function(color_idx)
            return "\27[38;5;"..color_idx.."m"
         end)
      .."\27[0m"
   )
end

--- generate from the script on the first topic
c = '@x021G@x020r@x020a@x055d@x055i@x054e@x090n@x089t@x125 @x125T@x160e@x160x@x196t'

str = print_with_colors(c)

print(str)  -- the result is below
str = 'Gradient Text '

str = string.char(27)..str

f=createForm()
pb=createPaintBox(f)
pb.Canvas.Font.Size=20
pb.Align='alClient'

local rect={}
rect.Left=0
rect.Top=0
rect.Right=pb.Width
rect.Bottom=pb.Height

pb.Repaint()
pb.Canvas.textRect(rect,0,0,str)



Capture.JPG
 Description:
 Filesize:  14.65 KB
 Viewed:  2147 Time(s)

Capture.JPG



gradient text.png
 Description:
 Filesize:  183.68 KB
 Viewed:  2154 Time(s)

gradient text.png



_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL


Last edited by Corroder on Mon Sep 14, 2020 3:49 am; edited 1 time in total
Back to top
View user's profile Send private message
.lua
Expert Cheater
Reputation: 1

Joined: 13 Sep 2018
Posts: 189

PostPosted: Mon Sep 14, 2020 3:16 am    Post subject: Reply with quote

Corroder wrote:
@Lua, thanks for the info. Yes, I knew and I have DB script to generate Rainbow Text as your example. But what I want to do is like attached image.
It's really enjoyable to present your text like this. I like it very much. I hope you can complete the code as soon as possible。
You can refer to this post below:
https://forum.cheatengine.org/viewtopic.php?t=578415&postdays=0&postorder=asc&start=0
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Mon Sep 14, 2020 3:53 am    Post subject: Reply with quote

@Lua, see the edit post on my last post before. I think just need to make little adjustment to get perfect result. I hope DB or someone expert will helps.
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
.lua
Expert Cheater
Reputation: 1

Joined: 13 Sep 2018
Posts: 189

PostPosted: Mon Sep 14, 2020 4:31 am    Post subject: Reply with quote

Corroder wrote:
@Lua, see the edit post on my last post before. I think just need to make little adjustment to get perfect result. I hope DB or someone expert will helps.

Maybe you need to use this:Change each pixel
Code:
Canvas.setPixel()
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Mon Sep 14, 2020 4:38 am    Post subject: Reply with quote

Maybe, but I think use get and set pixels giving extreme slow process.
I'm just playing around CE Lua graphics and learn how it works, especially with canvas with some tricks. Example, to get out lines text or glowing text, like this script below:

Code:
if f then f.destroy() end

f = createForm()
f.setSize(400,170)
f.Position = 'poScreenCenter'

pb=createPaintBox(f)
pb.Left,pb.Top,pb.Width,pb.Heigth = 0,30,400,170

f.OnPaint = function()
  Canvas = f.Canvas
  Canvas.GradientFill(0,0,f.Width, f.Height, 10263708, 197379, 1)
end

pb.OnPaint = function()
  local rect, mytext
  local rect={}
  rect.Left=0
  rect.Top=0
  rect.Right=pb.Width
  rect.Bottom=pb.Height

  Canvas = pb.Canvas
  mytext = 'Hello the world, Who am I ?'
  Canvas.Font.Size = 20
  Canvas.Font.Height = 42
  Canvas.Font.Color = 16777215

test1 = [[
  Canvas.TextRect(rect, 20 - 3, pb.Height - 80 - 0, mytext)
  Canvas.TextRect(rect, 20 + 3, pb.Height - 80 + 0, mytext)
  Canvas.TextRect(rect, 20 + 0, pb.Height - 80 - 3, mytext)
  Canvas.TextRect(rect, 20 - 0, pb.Height - 80 + 3, mytext)

  Canvas.TextRect(rect, 20 - 3, pb.Height - 80 - 3, mytext)
  Canvas.TextRect(rect, 20 + 3, pb.Height - 80 + 3, mytext)
  Canvas.TextRect(rect, 20 + 3, pb.Height - 80 - 3, mytext)
  Canvas.TextRect(rect, 20 - 3, pb.Height - 80 + 3, mytext)
  ]]

  Canvas.TextRect(rect, 10 - 1, pb.Height - 80 - 1, mytext)
  Canvas.TextRect(rect, 10 + 1, pb.Height - 80 + 1, mytext)
  Canvas.TextRect(rect, 10 + 1, pb.Height - 80 - 1, mytext)
  Canvas.TextRect(rect, 10 - 1, pb.Height - 80 + 1, mytext)

  Canvas.Font.Color = 1189470
  Canvas.TextRect(rect, 10, pb.Height - 80, mytext)
end




Capture.JPG
 Description:
Glow / Outline Text
 Filesize:  18.66 KB
 Viewed:  2136 Time(s)

Capture.JPG



_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
.lua
Expert Cheater
Reputation: 1

Joined: 13 Sep 2018
Posts: 189

PostPosted: Mon Sep 14, 2020 7:04 am    Post subject: Reply with quote

I don't know the color gradient algorithm, so 5 doesn't look good. The next step is how to speed up the rendering process and solve the color gradient algorithm。
Code:
if f==nil then f=createForm()  end
Lab=createLabel(f)
Lab.Font.Size=40
Lab.Font.Style='fsBold'
Lab.Font.Name='Trebuchet MS'
Lab.Font.Color=0x555555
Lab.Caption='0123456789'

PixelX={}   PixelY={}

i=0xf55555
function imgc(Thread2)
  sleep(1)
  for x=0,Lab.Canvas.Width-1 do  i=i+1
    for y=0,Lab.Canvas.Height-1 do
      if Lab.Canvas.getPixel(x,y)==0x555555 then
        Lab.Canvas.setPixel(x-2,y-2,i)
        --print(i)
        --table.insert(PixelX,x)
        --table.insert(PixelY,y)
      end
    end
  end
end
Thread2=createThread(imgc)

I have an idea: as long as I save the X and Y retrieved for the first time to the array, I will directly operate on the array next time, although the first time is special (I also used threads, otherwise it will be stuck)



textcolor.gif
 Description:
 Filesize:  106.48 KB
 Viewed:  2111 Time(s)

textcolor.gif


Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Mon Sep 14, 2020 9:15 am    Post subject: Reply with quote

Good!. I think just make different 1 or 2 point between label color value and i value.

Code:
Lab.Font.Color=   10921638
Lab.Caption='0123456789'

i =   10921636


function imgc(Thread2)
  sleep(1)
  for x=0,Lab.Canvas.Width-1 do
      i = i + 1
      for y=0,Lab.Canvas.Height-1 do
          if Lab.Canvas.getPixel(x,y)==   10921638 then
             Lab.Canvas.setPixel(x-1,y-1,i)
          end
      end
  end
end

Thread2=createThread(imgc)


Also, because you use createThread() then try to change the label caption to something else. I also afraid using createThread will consume CPU if not set a condition to kill/terminate the thread.

Anyhow, that is nice try. Good luck.

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

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Tue Sep 15, 2020 6:04 am    Post subject: Reply with quote

I am done with this. Just using ANSI 256 color escape code pattern.
And I am no need to worry to slow or consume my CPU.

Code:
-- ref ANSI 256 color = https://robotmoon.com/256-colors/
str = '\27[38;5;34mG\27[38;5;35mr\27[38;5;36ma\27[38;5;37md\27[38;5;38mi\27[38;5;39me\27[38;5;50mn\27[38;5;51mt\27'

if f then f.Destroy() end
f=createForm()
pb=createPaintBox(f)
pb.Canvas.Font.Size=52
pb.Align='alClient'

local rect={}
rect.Left=0
rect.Top=0
rect.Right=pb.Width
rect.Bottom=pb.Height

pb.Repaint()
pb.Canvas.Font.Style='fsBold'
pb.Canvas.Font.Name='Trebuchet MS'
pb.Canvas.Font.Color = 0
pb.Canvas.textRect(rect,16,54,'Gradient')
pb.Canvas.textRect(rect,12,50,str)



ANSI 256 Gradient.JPG
 Description:
My ANSI 256 Color Gradient Text
 Filesize:  20.08 KB
 Viewed:  2040 Time(s)

ANSI 256 Gradient.JPG



_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
.lua
Expert Cheater
Reputation: 1

Joined: 13 Sep 2018
Posts: 189

PostPosted: Tue Sep 15, 2020 8:26 am    Post subject: Reply with quote

I also think it's better to adopt the method of dark byte, only to understand the gradient algorithm, and then to customize the color
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Tue Sep 15, 2020 5:46 pm    Post subject: Reply with quote

Yes DB method is use generating ANSI escape code pattern from RGB values. The ANSI color usually use on print on terminal also have 16, 32, 64, 256 color maps.

The thing need to do is to substitute the escape string to CE Lua format, like using string.char() and of course need to linear algorithm for gradient color.

Example: (I am use xterm 256 colors ANSI escape code pattern)

Code:
\27[38;5;34mG\27[38 ; 5;35mr\27[38;5;36ma


\27 mean the ESC character or = string.char(27)
[38; is the foreground color
5 is the attribute
34m is background color
G is the letter G

Complete:

Code:
-- attributes
    reset = 0,
    clear = 0,
    bright = 1,
    dim = 2,
    underscore = 4,
    blink = 5,
    reverse = 7,
    hidden = 8,

-- foreground
    black = 30,
    red = 31,
    green = 32,
    yellow = 33,
    blue = 34,
    magenta = 35,
    cyan = 36,
    white = 37,

-- background
    onblack = 40,
    onred = 41,
    ongreen = 42,
    onyellow = 43,
    onblue = 44,
    onmagenta = 45,
    oncyan = 46,
    onwhite = 47,


The script on my first topic is trying to generated ANSI Esc Code to Lua format plus linear gradient algorithm.

_________________
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