Module:Sandbox/Sameboat/m2 explained

local getArgs = require('Module:Arguments').getArgs local p =

local function makeInvokeFunction(funcName) return function (frame) local args = getArgs(frame,) return p[funcName](args) endend

p.spectrum = makeInvokeFunction('_spectrum')function p._spectrum(args) local list = mw.text.split(args[1], '\n') local width = tonumber(args[2]) or tonumber(args.width) or 768 local pos,length,rgb,red,green,blue,gray,neargray,title local result = '

' for i1, v1 in ipairs(list) do for i2, v2 in ipairs(mw.text.split(v1, '\\')) do -- i.1:Name; i.2:RGB if (i2 % 2

1) then title = v2 result = result .. '

 ' elseif (i2 % 2

0) then if not (string.len(v2)

6 or string.len(v2)

3) then result = '

Invalid RGB value. Please use "RRGGBB" or "RGB" formats and all numbers must be converted to hexadecimal.

' else if string.len(v2)

6 then red, green, blue = v2:match("(..)(..)(..)") else red, green, blue = v2:match("(.)(.)(.)") red = red .. red green = green .. green blue = blue .. blue end red = tonumber(red, 16) green = tonumber(green, 16) blue = tonumber(blue, 16) rgb = v2 if red

green and red

blue then -- grayscale gray = true pos = 0 result = result .. '

' else if red > green and red > blue then -- red if blue > green then pos = width - (width / 6) * (blue - green) / (red - green) -- magenta to red elseif green > blue then pos = (width / 6) * (green - blue) / (red - blue) -- red to yellow else pos = 0 end elseif green > red and green > blue then -- green if red > blue then pos = width / 3 - (width / 6) * (red - blue) / (green - blue) -- yellow to green elseif blue > red then pos = width / 3 + (width / 6) * (blue - red) / (green - red) -- green to cyan else pos = width / 3 end elseif blue > red and blue > green then -- blue if green > red then pos = width * 2 / 3 - (width / 6) * (green - red) / (blue - red) -- cyan to blue elseif red > green then pos = width * 2 / 3 + (width / 6) * (red - green) / (blue - green) -- blue to magenta else pos = width * 2 / 3 end elseif red

green then -- yellow pos = width / 6 elseif green

blue then -- cyan pos = width / 2 elseif blue

red then -- magenta pos = width * 5 / 6 end if (math.abs(red - blue) < 20) and (math.abs(red - green) < 20) and (math.abs(green - blue) < 20) then -- near grayscale neargray = true end pos = math.ceil(pos) - 5 if pos <= 0 then else result = result .. '

' end result = result .. '

' end if pos < 0 then pos = 5 else pos = '-' .. pos end result = result .. '

     ' .. title .. ': ' .. string.upper(rgb) .. ' ' if gray

true then result = result .. '

(grayscale) ' elseif neargray

true then result = result .. '

(near grayscale) ' end gray = false neargray = false result = result .. '

' end end end end result = result .. '

 

' local colori = for i, v in ipairs(colori) do local colorin = '

' .. v .. '

' if width < 768 then if (i % 2)

1 then result = result .. colorin else end else result = result .. colorin end end return result .. '

'end

return p