Tocolor: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
Line 52: Line 52:
</syntaxhighlight>
</syntaxhighlight>


==The opposite function==  
==The opposite function==
This function is the opposite of tocolor, it turns a color back into R, G, B, A components.
<syntaxhighlight lang="lua">
function colorToRGBA( color )


local str = string.format( "%x", color )
See [http://wiki.multitheftauto.com/index.php?title=ColorToRGBA colorToRBGA]
return
tonumber( "0x" .. str:sub( 3, 4 ) ),
tonumber( "0x" .. str:sub( 5, 6 ) ),
tonumber( "0x" .. str:sub( 7, 8 ) ),
tonumber( "0x" .. str:sub( 1, 2 ) )
end
 
-- or
 
function colorToRGBA( color )
 
local a, r, g, b = getColorFromString( string.format( "#%x", color ) )
return r, g, b, a
 
end
 
 
local myColor = tocolor( 110, 180, 230 )
 
local R, G, B, A = colorToRGBA( myColor )
-- R is 110, G is 180, B is 230, A is 255
</syntaxhighlight>


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

Revision as of 21:38, 16 January 2013

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

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

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.

The opposite function

See colorToRBGA

See Also