Advertisement
BugFix

ShowHexColorFromCursor

Jul 20th, 2015
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.69 KB | None | 0 0
  1. -- TIME_STAMP   2015-07-20 15:18:51   v 0.2
  2.  
  3.  
  4. --[[
  5. Save the file.
  6. At first, make an entry in your SciTEStartup.lua
  7.  
  8. LoadLuaFile("ShowHexColorFromCursor.lua", "C:\\Your Path\\with Backslash\\")
  9.  
  10. ]]
  11.  
  12.  
  13.  
  14.  
  15. --[[
  16. Select a free command-number from your SciTEUser.properties.
  17. Customize the following settings with this number.
  18.  
  19.  
  20. # 13 Show HexColor
  21. command.name.13.*=Show Color From Cursor
  22. command.13.*=dostring ShowHexColorFromCursor()
  23. command.mode.13.*=subsystem:lua
  24. command.shortcut.13.*=Ctrl+Shift+F11
  25.  
  26.  
  27. Set the cursor in the Hex-value, press the hotkey.
  28. Above the value a Call tip appears. The background color corresponds to the hex value.
  29. A possible alpha component is ignored.
  30.  
  31. Be recognized AutoIt hex color code "0x12AB34" and also HTML hex color code "#12AB34".
  32. ]]
  33.  
  34. function ShowHexColorFromCursor()
  35.     local iLen = 8
  36.     local function isHexChar(_asc)
  37.         local sChar = string.char(_asc)
  38.         if sChar == '#' then iLen = 7 end
  39.         if sChar:find('[#x0-9a-fA-F]') then return true else return false end
  40.     end
  41.     local cursor = editor.CurrentPos
  42.     local beginPos, endPos = cursor, cursor
  43.     local pattHex2 = '[0-9a-fA-F][0-9a-fA-F]'
  44.     while isHexChar(editor.CharAt[beginPos-1]) do
  45.         beginPos = beginPos - 1
  46.     end
  47.     while isHexChar(editor.CharAt[endPos]) do
  48.         endPos = endPos + 1
  49.     end
  50.     if beginPos ~= endPos then
  51.         if endPos - beginPos > iLen then
  52.             editor:SetSelection(beginPos + iLen, beginPos)
  53.         elseif endPos - beginPos == iLen then
  54.             editor:SetSelection(endPos, beginPos)
  55.         else
  56.             return
  57.         end
  58.         local R,G,B = tostring(editor:GetSelText()):match('('..pattHex2..')('..pattHex2..')('..pattHex2..')$')
  59.         scite.SendEditor(SCI_CALLTIPSHOW, beginPos, (' '):rep(iLen))
  60.         scite.SendEditor(SCI_CALLTIPSETHLT, 0, iLen)
  61.         scite.SendEditor(SCI_CALLTIPSETBACK, tonumber(string.format('0x%s%s%s', B,G,R)))
  62.     end
  63. end
  64.  
  65.  
  66. --[[
  67. To have a preview for back and fore color:
  68. - Write in one line first the back color, than the fore color (i. e. as comment: "; 0xDEDEDE 0x000080") OR
  69.   have this values inside a function call: "_AnyFunction($param1, $param2, 0xDEDEDE, $param3 0x000080)".
  70.   If the order inside the call is reverse (first hex value is fore color), you can call the function with Flag "_fFore1st=true"
  71. - No other color value may be included in this line. If any - the first and second color will used.
  72. - Set the cursor in this line and hit the Hotkey.
  73. - A Calltip appears with the back color and the text "FORE-COLOR" with color of the fore value.
  74. - If only one color value was find in this line, this value will used as back color or, if Flag is "true", as fore color.
  75.   In this cases the fore color is set to black and with Flag the back color is the default GUI back color "0xF0F0F0"
  76.  
  77. For use with AutoIt color values only.
  78.  
  79. To have both calls (w/wo flag) make two commands:
  80.  
  81. # 11 Preview Back and Fore Color / first color value is back color
  82. command.name.11.*.au3=Preview Back and Fore Color
  83. command.11.*.au3=dostring PreviewBackForeColor()
  84. command.mode.11.*.au3=subsystem:lua,savebefore:no
  85. command.shortcut.11.*.au3=Ctrl+Shift+F12
  86.  
  87. # 16 Preview Fore and Back Color / first color value is fore color
  88. command.name.16.*.au3=Preview Fore and Back Color
  89. command.16.*.au3=dostring PreviewBackForeColor(true)
  90. command.mode.16.*.au3=subsystem:lua,savebefore:no
  91. command.shortcut.16.*.au3=Ctrl+Alt+F12
  92.  
  93. ]]
  94.  
  95. function PreviewBackForeColor(_fFore1st)
  96.     local pattHex = '()0x([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])'
  97.     local iBackCol, iForeCol = 0xF0F0F0, 0x000000
  98.     local cursor = editor.CurrentPos
  99.     local sLine, iColumn = editor:GetCurLine()
  100.     local iLineStartPos = cursor - iColumn
  101.     local tMatch, beginPos = {}, nil
  102.     for s, r, g, b in sLine:gmatch(pattHex) do
  103.         if beginPos == nil then beginPos = s end
  104.         local t = {} t['R']=r t['G']=g t['B']=b
  105.         table.insert(tMatch, t)
  106.     end
  107.     if #tMatch == 0 then
  108.         return
  109.     elseif #tMatch == 1 then
  110.         if _fFore1st == true then
  111.             iForeCol = tonumber(string.format('0x%s%s%s', tMatch[1].B, tMatch[1].G, tMatch[1].R))
  112.         else
  113.             iBackCol = tonumber(string.format('0x%s%s%s', tMatch[1].B, tMatch[1].G, tMatch[1].R))
  114.         end
  115.     else
  116.         if _fFore1st == true then
  117.             iForeCol = tonumber(string.format('0x%s%s%s', tMatch[1].B, tMatch[1].G, tMatch[1].R))
  118.             iBackCol = tonumber(string.format('0x%s%s%s', tMatch[2].B, tMatch[2].G, tMatch[2].R))
  119.         else
  120.             iForeCol = tonumber(string.format('0x%s%s%s', tMatch[2].B, tMatch[2].G, tMatch[2].R))
  121.             iBackCol = tonumber(string.format('0x%s%s%s', tMatch[1].B, tMatch[1].G, tMatch[1].R))
  122.         end
  123.     end
  124.     scite.SendEditor(SCI_CALLTIPSHOW, iLineStartPos + beginPos, ' FORE-COLOR ')
  125.     scite.SendEditor(SCI_CALLTIPSETHLT, 0, 12)
  126.     scite.SendEditor(SCI_CALLTIPSETBACK, iBackCol)
  127.     scite.SendEditor(SCI_CALLTIPSETFOREHLT, iForeCol)
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement