Guest User

dxDrawTextColorCoded

a guest
Oct 20th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. function dxDrawTextColorCoded ( text, left, top, alpha, scale, font, clip, postGUI )
  2.     if text and tostring(text) and left and tonumber(left) and top and tonumber(top) then
  3.         if not scale or not tonumber(scale) then scale = 1 end
  4.         if not font or not tostring(font) then font = "default" end
  5.    
  6.         local textX, textY = left, top
  7.         local textH = dxGetFontHeight ( scale, font )
  8.        
  9.         local textClear = string.gsub ( text, "#%x%x%x%x%x%x", "" )
  10.         local textTable = {}
  11.  
  12.         local splitTable = split ( text, 35 )
  13.         if #splitTable == 1 then
  14.             local codeStart, codeStop = string.find ( text, "#%x%x%x%x%x%x" )
  15.             if codeStart and codeStop then
  16.                 local r, g, b = hexToRGB ( string.sub ( text, codeStart, codeStop ) )
  17.                 table.insert ( textTable, { textClear, r, g, b } )
  18.             else
  19.                 table.insert ( textTable, { textClear, 255, 255, 255 } )
  20.             end
  21.         else
  22.             for i, string in pairs ( splitTable ) do
  23.                 if i == 1 then
  24.                     local codeStart, codeStop = string.find ( text, "#%x%x%x%x%x%x" )
  25.                     if codeStart and codeStop and codeStop < #string+1 then
  26.                         local r, g, b = hexToRGB ( string.sub ( text, codeStart, codeStop ) )
  27.                         table.insert ( textTable, { string.gsub ( "#"..string, "#%x%x%x%x%x%x", "" ), r, g, b } )
  28.                     else
  29.                         table.insert ( textTable, { string, 255, 255, 255 } )
  30.                     end
  31.                 else
  32.                     local newString = "#"..string
  33.                     local r, g, b = hexToRGB ( string.sub ( newString, 1, 7 ) )
  34.                     table.insert ( textTable, { string.gsub ( newString, "#%x%x%x%x%x%x", "" ), r, g, b } )
  35.                 end
  36.             end
  37.         end        
  38.  
  39.         local startX = 0
  40.         for i, stringData in pairs ( textTable ) do
  41.             local string, r, g, b = stringData[1], stringData[2], stringData[3], stringData[4]
  42.             if #string > 0 then
  43.                 local stringWidth = dxGetTextWidth ( string, scale, font )
  44.                 dxDrawText ( string, textX+startX, textY, textX+startX+stringWidth, textY+textH, tocolor ( r, g, b, alpha ), scale, font, "left", "top", clip, false, postGUI )
  45.                 startX = startX + stringWidth
  46.             end
  47.         end
  48.     else
  49.         return false
  50.     end
  51. end
  52.  
  53. function hexToRGB ( hex )
  54.     hex = hex:gsub("#","")
  55.     return tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6))
  56. end
  57.  
Advertisement
Add Comment
Please, Sign In to add comment