Module:LyricColor

From Heterodontosaurus Balls

Documentation for this module may be created at Module:LyricColor/doc

local stringToArray = require('Module:ProcessingFunctions').stringToArray
local data = mw.loadData('Module:LyricColor/data')

local p = {}

-- Formats names for hover
local function formatCharacters(list)
	local newChara = {}
	
	for _, character in ipairs(list) do
		character = string.lower(character)
		
		-- Check if the name is a group
		if data.group[character] ~= nil then
			for _, member in ipairs(data.group[character]) do
				if data.display_name[member] ~= nil then
					table.insert(newChara, data.display_name[member])
				else
					member = string.gsub(string.lower(member),'^%a',string.upper)
					table.insert(newChara, member)
				end
			end
		else
			-- Check if the name has display rules
			if data.display_name[character] ~= nil then
				table.insert(newChara, data.display_name[character])
			
			-- Otherwise capitalize first letter
			else
				character = string.gsub(string.lower(character),'^%a',string.upper)
				table.insert(newChara, character)
			end
		end
	end
	
	return table.concat(newChara, ", ")
end

-- Fixes solo lines on mobile for horizontal gradients
local function horizontalFix(list)
  t= {}
  for i=1,#list do
    if i==1 and #list==1 then
      table.insert(t, list[i])
      table.insert(t, list[i])
    else
      table.insert(t, list[i])
    end
  end
  return t
end

-- Adjusts gradient based on number of characters for vertical gradients
local function verticalFix(list)
  t= {}
  for i=1,#list do
    -- First character
    if i==1 then
      -- First of one
      if #list==1 then
      	table.insert(t, list[i])
      	table.insert(t, list[i])
      -- First of two
      elseif #list==2 then
        table.insert(t, list[i] .. " 40%")
        table.insert(t, "55%")
      -- First of three
      elseif #list==3 then
        table.insert(t, list[i] .. " 30%")
        table.insert(t, "40%")
      -- First of four
      elseif #list==4 then
        table.insert(t, list[i] .. " 30%")
      -- First of five
      elseif #list==5 then
        table.insert(t, list[i] .. " 25%")
      else
        table.insert(t, list[i])
      end
    
    -- Second character
    elseif i==2 then
      -- Second of two
      if #list==2 then
        table.insert(t, list[i] .. " 70%")
      -- Second of three
      elseif #list==3 then
        table.insert(t, list[i] .. " 55%")
        table.insert(t, "65%")
      -- Second of four
      elseif #list==4 then
        table.insert(t, list[i] .. " 40%")
      -- Second of five
      elseif #list==5 then
        table.insert(t, list[i] .. " 35%")
      else
        table.insert(t, list[i])
      end
    
    -- Third character
    elseif i==3 then
      -- Third of three
      if #list==3 then
        table.insert(t, list[i] .. " 80%")
      -- Third of four
      elseif #list==4 then
        table.insert(t, list[i] .. " 60%")
      -- Third of five
      elseif #list==5 then
        table.insert(t, list[i] .. " 50%")
      else
        table.insert(t, list[i])
      end
  
    -- Fourth character
    elseif i==4 then
      -- Fourth of four
      if #list==4 then
        table.insert(t, list[i] .. " 80%")
      -- Fifth of five
      elseif #list==5 then
        table.insert(t, list[i] .. " 60%")
      else
        table.insert(t, list[i])
      end
    
    -- Fifth character
    elseif i==5 and #list==5 then
      table.insert(t, list[i] .. " 75%")
    
    -- Sixth+ character
    else
      table.insert(t, list[i])
    end
  end
  return t
end

-- Assigns color codes
local function assignColor(list)
	colors = {}
	
    -- adds colors to list
    for i=1,#list do
    	-- makes name lowercase to prevent case sensitivity
    	local character = string.lower(list[i])
      
    	-- Check if the name is a group
    	if data.group[character] ~= nil then
    		for _, member in ipairs(data.group[character]) do
    			table.insert(colors, data.color[member])
    		end
    	elseif data.color[character] ~= nil then
    		table.insert(colors, data.color[character])
    	else
    		table.insert(colors, "oklch(80% 0.05 262.29)")
    	end
    end

    hexList = horizontalFix(colors)
    return table.concat(hexList, ", ")
end

-- Returns span
function p.lyricColor(frame)
	local characters = stringToArray(",")(frame.args[1])
	local colors = assignColor(characters)
	
	local formattedChara = formatCharacters(characters)
		
	local root = mw.html.create('span')
	root:tag('span')
		:addClass('text-color')
		:attr('title',formattedChara)
		:css({
			['background'] = 'linear-gradient(to right,' .. colors ..')',
			['-webkit-background-clip'] = 'text',
			['color'] = 'transparent'
		})
		:wikitext((frame.args[2]))
		
	return tostring(root)
end

return p