y453bh

(QUESTION) HOW TO DO THIS??

Recommended Posts

Hi community,

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

Share this post


Link to post
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
      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 by Santi

Share this post


Link to post
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

Share this post


Link to post
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

 

Share this post


Link to post
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.

Share this post


Link to post

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.