Copy Color as BBCode

Share your scripts and packages with other Mudlet users.
Post Reply
Khonsu
Posts: 2
Joined: Mon May 02, 2022 2:29 am

Copy Color as BBCode

Post by Khonsu »

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:

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]
and here's the script:

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")
Last edited by Khonsu on Fri May 06, 2022 11:38 am, edited 1 time in total.

User avatar
Vadi
Posts: 4986
Joined: Sat Mar 14, 2009 3:13 pm

Re: Copy Color as BBCode

Post by Vadi »

I think this is very cool, nice work! Post about in #packages on Discord as well.

User avatar
demonnic
Posts: 798
Joined: Sat Dec 05, 2009 3:19 pm

Re: Copy Color as BBCode

Post by demonnic »

I would maybe recommend changing the function name to something which speaks to what it does. copyAsBBCode or some such. otherwise you might clash with everyone else who started by copying it from the wiki but didn't change the function name =)

Khonsu
Posts: 2
Joined: Mon May 02, 2022 2:29 am

Re: Copy Color as BBCode

Post by Khonsu »

Oh yeah I guess I was only thinking about my use case, oops. I've updated the script above to have two uniquely named functions. I did slightly modify the rgbToHex part as well. I tested it again real quick and it all seems to be normal. Thank you for the help!

Post Reply