Page 1 of 1

Copy Color as BBCode

Posted: Thu May 05, 2022 3:23 am
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.


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)

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
        lastColor = color
        parsed = parsed .. "[/color][color=#" .. color .. "]" .. symbol
    if l ~= endRow then parsed = parsed end
    if parsed:sub(1,8) == "[/color]" then
        parsed = parsed:sub(9).."[/color]"
    end --if

registerAnonymousEventHandler("copyAsBBCode", "copyAsBBCode")

Re: Copy Color as BBCode

Posted: Thu May 05, 2022 4:48 pm
by Vadi
I think this is very cool, nice work! Post about in #packages on Discord as well.

Re: Copy Color as BBCode

Posted: Thu May 05, 2022 6:25 pm
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 =)

Re: Copy Color as BBCode

Posted: Fri May 06, 2022 11:40 am
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!