Garry's Mod Wiki

HSVToColor

table HSVToColor( number hue, number saturation, number value )

Description

Converts a color from HSV color space into RGB color space and returns a Color.

The returned color will not have the color metatable.

Issue Tracker: 2407

Arguments

1 number hue
The hue in degrees from 0-360.
2 number saturation
The saturation from 0-1.
3 number value
The value from 0-1.

Returns

1 table
The Color created from the HSV color space.

Example

A helper function for drawing rainbow text.

local function DrawRainbowText( frequency, str, font, x, y ) surface.SetFont( font ) surface.SetTextPos( x, y ) for i = 1, #str do local col = HSVToColor( i * frequency % 360, 1, 1 ) -- Providing 3 numbers to surface.SetTextColor rather surface.SetTextColor( col.r, col.g, col.b ) -- than a single color is faster surface.DrawText( string.sub( str, i, i ) ) end end -- Solid color rainbow, faster than example above local function DrawSimpleRainbowText( speed, str, font, x, y ) surface.SetFont( font ) surface.SetTextColor( HSVToColor( ( CurTime() * speed ) % 360, 1, 1 ) ) surface.SetTextPos( x, y ) surface.DrawText( str ) end hook.Add( "HUDPaint", "RainbowPuke", function() DrawRainbowText( 10, "Hello world! This is rainbow text.", "CloseCaption_Bold", 5, 5 ) DrawSimpleRainbowText( 100, "Hello world! This is rainbow text.", "CloseCaption_Bold", 5, 55 ) end )
Output:

Example

A helper function for printing rainbow text in the chat.

local function PrintRainbowText( frequency, str ) local text = {} local len = #text for i = 1, #str do text[len + 1] = HSVToColor( i * frequency % 360, 1, 1 ) text[len + 2] = string.sub( str, i, i ) len = len + 2 end -- Print to chat, also prints to console chat.AddText( unpack( text ) ) -- Uncomment this to print to console only, works serverside too -- MsgC( unpack( text ) ) end -- The higher the number, the quicker the color will change between each character PrintRainbowText( 10, "Hello world!" )
Output:

Page Links


Special Pages


Wikis

?

Render Time: 51ms

DB GetPage 8
Generate Html 20
SaveChanges (1) 8
Render Body 0
Render Sidebar 12