y453bh Posted March 26, 2020 Share Posted March 26, 2020 Hi community, How to make a script ( command ) for getting hex code from rgb and viceversa? thanks Link to comment
Santi Posted March 26, 2020 Share Posted March 26, 2020 (edited) function hex2rgb (hex) local hex = hex:gsub("#","") if hex:len() == 3 then return (tonumber("0x"..hex:sub(1,1))*17)/255, (tonumber("0x"..hex:sub(2,2))*17)/255, (tonumber("0x"..hex:sub(3,3))*17)/255 else return tonumber("0x"..hex:sub(1,2))/255, tonumber("0x"..hex:sub(3,4))/255, tonumber("0x"..hex:sub(5,6))/255 end end -- exemple usage: (NOT TESTED the command) but the function works fine addEventHandler("onPlayerCommand",root, function(command, player) if (command == "convert") then outputConsole("Result: " .. hex2rgb("#ffffff"),player) end end) Edited March 26, 2020 by Santi Link to comment
y453bh Posted March 27, 2020 Author Share Posted March 27, 2020 7 hours ago, Santi said: function hex2rgb (hex) local hex = hex:gsub("#","") if hex:len() == 3 then return (tonumber("0x"..hex:sub(1,1))*17)/255, (tonumber("0x"..hex:sub(2,2))*17)/255, (tonumber("0x"..hex:sub(3,3))*17)/255 else return tonumber("0x"..hex:sub(1,2))/255, tonumber("0x"..hex:sub(3,4))/255, tonumber("0x"..hex:sub(5,6))/255 end end -- exemple usage: (NOT TESTED the command) but the function works fine addEventHandler("onPlayerCommand",root, function(command, player) if (command == "convert") then outputConsole("Result: " .. hex2rgb("#ffffff"),player) end end) Not working Link to comment
#\_oskar_/# Posted March 27, 2020 Share Posted March 27, 2020 function hex2rgb (hex) local hex = hex:gsub("#","") for i=0,5 do if hex:len() == i then return error ('Error hex2rgb ('.. (hex)..')' ) end end r = tonumber("0x"..hex:sub(1,2)) or 0 g = tonumber("0x"..hex:sub(3,4)) or 0 b = tonumber("0x"..hex:sub(5,6)) or 0 return r..','..g..','..b end try _- i edited the code Link to comment
N3xT Posted March 28, 2020 Share Posted March 28, 2020 On 26/03/2020 at 19:36, Santi said: function hex2rgb (hex) local hex = hex:gsub("#","") if hex:len() == 3 then return (tonumber("0x"..hex:sub(1,1))*17)/255, (tonumber("0x"..hex:sub(2,2))*17)/255, (tonumber("0x"..hex:sub(3,3))*17)/255 else return tonumber("0x"..hex:sub(1,2))/255, tonumber("0x"..hex:sub(3,4))/255, tonumber("0x"..hex:sub(5,6))/255 end end -- exemple usage: (NOT TESTED the command) but the function works fine addEventHandler("onPlayerCommand",root, function(command, player) if (command == "convert") then outputConsole("Result: " .. hex2rgb("#ffffff"),player) end end) Use addCommandHandler to create a command, also the source of onPlayerCommand is the player. so you don't need to add player as a parameter. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now