# (QUESTION) HOW TO DO THIS??

## Recommended Posts

Hi community,

How to make a script ( command ) for getting hex code from rgb and viceversa? thanks

Posted (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
end
end

-- exemple usage: (NOT TESTED the command) but the function works fine

function(command, player)
if (command == "convert") then
outputConsole("Result: " .. hex2rgb("#ffffff"),player)
end
end)```

Edited by Santi

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
end
end

-- exemple usage: (NOT TESTED the command) but the function works fine

function(command, player)
if (command == "convert") then
outputConsole("Result: " .. hex2rgb("#ffffff"),player)
end
end)
```

Not working

Help!

```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

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
end
end

-- exemple usage: (NOT TESTED the command) but the function works fine

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.