Tocolor: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
 
(18 intermediate revisions by 3 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Client function}}  
{{Server client function}}
This function retrieves the hex number of a specified color, useful for the dx functions.
This function retrieves the hex number of a specified color, useful for the dx functions.
{{New feature/item|3.0156|1.5.5|13977|Added server-side.}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
int tocolor ( int red, int green, int blue [, int alpha = 255] )
int tocolor ( int red, int green, int blue [, int alpha = 255 ] )
</syntaxhighlight>
</syntaxhighlight>


Line 19: Line 20:
Returns a single value representing the color.
Returns a single value representing the color.


==Example==  
==Example==
This example displays the text "Tuna" in small at the top left side of your screen. The color of this text can be changed using a command.
<section name="Client" class="client" show="true">
This example displays the text "Tuna" in small at the top left side of your screen. The color of this text can be changed using the command /tunaColor.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Make some default colour
local tunaColor = tocolor(255, 0, 0, 255) -- Default color
R = 255
G = 255
B = 255
A = 255


-- This function draws the text, and the color is decided with tocolor(R, G, B, A)
-- This function draws the text
function drawDX()
local function drawTuna()
dxDrawText("Tuna", 10, 10, 100, 100, tocolor(R, G, B, A), 1)
dxDrawText("Tuna", 5, 5, 100, 100, tunaColor)
end
end
addEventHandler("onClientRender", root, drawDX)
addEventHandler("onClientRender", root, drawTuna)
-- This will make some DX text (Tuna) appear at the top left of your screen.


--Now to modify the R, G, B, and A with a command.
--This function handles the /tunaColor command, allowing players to set the color of tuna
function editRGBA(command, red, green, blue, alpha)
local function tunaColorCommand(command, red, green, blue, alpha)
if (tonumber(red) and tonumber(green) and tonumber(blue) and tonumber(alpha)) then -- Make sure the user entered all 4 args as numbers.
red, green, blue, alpha = tonumber(red), tonumber(green), tonumber(blue), tonumber(alpha) -- Convert all the args to numbers. NOTE: tonumber will return false if the arg is not provided/is not valid.
R = tonumber(red)
G = tonumber(green)
-- Remind the user of the proper syntax if they failed to provide all the args
B = tonumber(blue)
if not red or not green or not blue then
A = tonumber(alpha)
outputChatBox("* USAGE: /tunaColor [red] [green] [blue] [alpha]", 255, 0, 0)
-- tonumber() had to be used here else tocolor() didn't display the right color.
return
outputChatBox("Color of Tuna set to the color of this text, with "..A.." alpha.", R, G, B)
end
end
-- Make the alpha arg optional
if not alpha then
alpha = 255
end
-- Update the color
tunaColor = tocolor(red, green, blue, alpha)
end
end
addCommandHandler("setcoloroftuna", editRGBA) -- setcoloroftuna command.
addCommandHandler("tunaColor", tunaColorCommand)
-- Example /setcoloroftuna 255 0 0 255 - for red.
-- Example /setcoloroftuna 255 0 0 255 - for red.
</syntaxhighlight>
</syntaxhighlight>
</section>
==Changelog==
{{ChangelogHeader}}
{{ChangelogItem|1.5.5-r13977|Added server-side}}


==See Also==
==See Also==
{{Client_utility_functions}}
{{Utility_functions}}

Latest revision as of 13:49, 6 August 2018

This function retrieves the hex number of a specified color, useful for the dx functions. Added server-side.

Syntax

int tocolor ( int red, int green, int blue [, int alpha = 255 ] )

Required Arguments

  • red: The amount of red in the color (0-255).
  • green: The amount of green in the color (0-255).
  • blue: The amount of blue in the color (0-255).

Optional Arguments

  • alpha: The amount of alpha in the color (0-255).

Returns

Returns a single value representing the color.

Example

Click to collapse [-]
Client

This example displays the text "Tuna" in small at the top left side of your screen. The color of this text can be changed using the command /tunaColor.

local tunaColor = tocolor(255, 0, 0, 255) -- Default color

-- This function draws the text
local function drawTuna()
	dxDrawText("Tuna", 5, 5, 100, 100, tunaColor)
end
addEventHandler("onClientRender", root, drawTuna)

--This function handles the /tunaColor command, allowing players to set the color of tuna
local function tunaColorCommand(command, red, green, blue, alpha)
	red, green, blue, alpha = tonumber(red), tonumber(green), tonumber(blue), tonumber(alpha) -- Convert all the args to numbers. NOTE: tonumber will return false if the arg is not provided/is not valid.
	
	-- Remind the user of the proper syntax if they failed to provide all the args
	if not red or not green or not blue then
		outputChatBox("* USAGE: /tunaColor [red] [green] [blue] [alpha]", 255, 0, 0)
		return
	end
	
	-- Make the alpha arg optional
	if not alpha then
		alpha = 255
	end
	
	-- Update the color
	tunaColor = tocolor(red, green, blue, alpha)
end
addCommandHandler("tunaColor", tunaColorCommand)
-- Example /setcoloroftuna 255 0 0 255 - for red.

Changelog

Version Description
1.5.5-r13977 Added server-side

See Also