Copy Color as BBCode
Posted: Thu May 05, 2022 3:23 am
This is probably a really small niche thing that won't apply to a lot of people, but I had to share it just in case it helps someone else out. I did not know _any_ Lua about a week ago, and I still don't know a lot. I found most of the code for this in the technical manual under addMouseEvent and modified it just enough to copy color codes for posting bulletin boards. We use this on our MUD for roleplayers to upload logs to the forums. Its as simple as installing this into the scripts tab on Mudlet, and right click > RPP Copy > Paste
For example copying in game:
-<OOC>- Khonsu whistles innocently and starts to walk away.
becomes:
and here's the script:
For example copying in game:
-<OOC>- Khonsu whistles innocently and starts to walk away.
becomes:
Code: Select all
[color=#008080]-[/color][color=#00FFFF]<[/color][color=#FFFFFF]OOC[/color][color=#00FFFF]>[/color][color=#008080]- Khonsu whistles innocently and starts to walk away.[/color]
Code: Select all
addMouseEvent("RPP Copy", "copyAsBBCode")
function rgbToRPHex(r,g,b)
local rgb = (r * 0x10000) + (g * 0x100) + b
return string.format("%06X", rgb)
end
function copyAsBBCode(event, menu, window, startCol, startRow, endCol, endRow)
-- Check whether there's an actual selection
if startCol == endCol and startRow == endRow then return end
local parsed = ""
local lastColor = nil
-- Loop through each symbol within the range
for l = startRow, endRow do
local cStart = l == startRow and startCol or 0
moveCursor(window, cStart, l)
local cEnd = l == endRow and endCol or #getCurrentLine() - 1
for c = cStart, cEnd do
selectSection(window, c, 1)
local symbol = getSelection(window) or ""
-- Convert the foreground color to a hex format, suitable for hecho
local color = rgbToRPHex(getFgColor(window))
-- Don't repeat the color if previous one was the same
if color == lastColor then
parsed = parsed .. symbol
else
lastColor = color
parsed = parsed .. "[/color][color=#" .. color .. "]" .. symbol
end
end
if l ~= endRow then parsed = parsed end
end
if parsed:sub(1,8) == "[/color]" then
parsed = parsed:sub(9).."[/color]"
end --if
setClipboardText(parsed)
end
registerAnonymousEventHandler("copyAsBBCode", "copyAsBBCode")