Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- TIME_STAMP 2015-07-20 15:18:51 v 0.2
- --[[
- Save the file.
- At first, make an entry in your SciTEStartup.lua
- LoadLuaFile("ShowHexColorFromCursor.lua", "C:\\Your Path\\with Backslash\\")
- ]]
- --[[
- Select a free command-number from your SciTEUser.properties.
- Customize the following settings with this number.
- # 13 Show HexColor
- command.name.13.*=Show Color From Cursor
- command.13.*=dostring ShowHexColorFromCursor()
- command.mode.13.*=subsystem:lua
- command.shortcut.13.*=Ctrl+Shift+F11
- Set the cursor in the Hex-value, press the hotkey.
- Above the value a Call tip appears. The background color corresponds to the hex value.
- A possible alpha component is ignored.
- Be recognized AutoIt hex color code "0x12AB34" and also HTML hex color code "#12AB34".
- ]]
- function ShowHexColorFromCursor()
- local iLen = 8
- local function isHexChar(_asc)
- local sChar = string.char(_asc)
- if sChar == '#' then iLen = 7 end
- if sChar:find('[#x0-9a-fA-F]') then return true else return false end
- end
- local cursor = editor.CurrentPos
- local beginPos, endPos = cursor, cursor
- local pattHex2 = '[0-9a-fA-F][0-9a-fA-F]'
- while isHexChar(editor.CharAt[beginPos-1]) do
- beginPos = beginPos - 1
- end
- while isHexChar(editor.CharAt[endPos]) do
- endPos = endPos + 1
- end
- if beginPos ~= endPos then
- if endPos - beginPos > iLen then
- editor:SetSelection(beginPos + iLen, beginPos)
- elseif endPos - beginPos == iLen then
- editor:SetSelection(endPos, beginPos)
- else
- return
- end
- local R,G,B = tostring(editor:GetSelText()):match('('..pattHex2..')('..pattHex2..')('..pattHex2..')$')
- scite.SendEditor(SCI_CALLTIPSHOW, beginPos, (' '):rep(iLen))
- scite.SendEditor(SCI_CALLTIPSETHLT, 0, iLen)
- scite.SendEditor(SCI_CALLTIPSETBACK, tonumber(string.format('0x%s%s%s', B,G,R)))
- end
- end
- --[[
- To have a preview for back and fore color:
- - Write in one line first the back color, than the fore color (i. e. as comment: "; 0xDEDEDE 0x000080") OR
- have this values inside a function call: "_AnyFunction($param1, $param2, 0xDEDEDE, $param3 0x000080)".
- If the order inside the call is reverse (first hex value is fore color), you can call the function with Flag "_fFore1st=true"
- - No other color value may be included in this line. If any - the first and second color will used.
- - Set the cursor in this line and hit the Hotkey.
- - A Calltip appears with the back color and the text "FORE-COLOR" with color of the fore value.
- - 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.
- In this cases the fore color is set to black and with Flag the back color is the default GUI back color "0xF0F0F0"
- For use with AutoIt color values only.
- To have both calls (w/wo flag) make two commands:
- # 11 Preview Back and Fore Color / first color value is back color
- command.name.11.*.au3=Preview Back and Fore Color
- command.11.*.au3=dostring PreviewBackForeColor()
- command.mode.11.*.au3=subsystem:lua,savebefore:no
- command.shortcut.11.*.au3=Ctrl+Shift+F12
- # 16 Preview Fore and Back Color / first color value is fore color
- command.name.16.*.au3=Preview Fore and Back Color
- command.16.*.au3=dostring PreviewBackForeColor(true)
- command.mode.16.*.au3=subsystem:lua,savebefore:no
- command.shortcut.16.*.au3=Ctrl+Alt+F12
- ]]
- function PreviewBackForeColor(_fFore1st)
- 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])'
- local iBackCol, iForeCol = 0xF0F0F0, 0x000000
- local cursor = editor.CurrentPos
- local sLine, iColumn = editor:GetCurLine()
- local iLineStartPos = cursor - iColumn
- local tMatch, beginPos = {}, nil
- for s, r, g, b in sLine:gmatch(pattHex) do
- if beginPos == nil then beginPos = s end
- local t = {} t['R']=r t['G']=g t['B']=b
- table.insert(tMatch, t)
- end
- if #tMatch == 0 then
- return
- elseif #tMatch == 1 then
- if _fFore1st == true then
- iForeCol = tonumber(string.format('0x%s%s%s', tMatch[1].B, tMatch[1].G, tMatch[1].R))
- else
- iBackCol = tonumber(string.format('0x%s%s%s', tMatch[1].B, tMatch[1].G, tMatch[1].R))
- end
- else
- if _fFore1st == true then
- iForeCol = tonumber(string.format('0x%s%s%s', tMatch[1].B, tMatch[1].G, tMatch[1].R))
- iBackCol = tonumber(string.format('0x%s%s%s', tMatch[2].B, tMatch[2].G, tMatch[2].R))
- else
- iForeCol = tonumber(string.format('0x%s%s%s', tMatch[2].B, tMatch[2].G, tMatch[2].R))
- iBackCol = tonumber(string.format('0x%s%s%s', tMatch[1].B, tMatch[1].G, tMatch[1].R))
- end
- end
- scite.SendEditor(SCI_CALLTIPSHOW, iLineStartPos + beginPos, ' FORE-COLOR ')
- scite.SendEditor(SCI_CALLTIPSETHLT, 0, 12)
- scite.SendEditor(SCI_CALLTIPSETBACK, iBackCol)
- scite.SendEditor(SCI_CALLTIPSETFOREHLT, iForeCol)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement