Revision Difference
Global.HSVToColor#526420
<function name="HSVToColor" parent="Global" type="libraryfunc">
<description>
Converts a color from [HSV color space](https://en.wikipedia.org/wiki/HSL_and_HSV) into RGB color space and returns a <page>Color</page>.
<bug issue="2407">The returned color will not have the color metatable.</bug>
</description>
<realm>Shared and Menu</realm>
<args>
<arg name="hue" type="number">The hue in degrees from 0-360.</arg>
<arg name="saturation" type="number">The saturation from 0-1.</arg>
<arg name="value" type="number">The value from 0-1.</arg>
</args>
<rets>
<ret name="" type="table">The <page>Color</page> created from the HSV color space.</ret>
</rets>
</function>
<example>
<description>A helper function for drawing rainbow text.</description>
<code>
local function DrawRainbowText( frequency, str, font, x, y )
surface.SetFont( font )⤶
⤶
for i = 1, #str do⤶
⤶
surface.SetFont( font )
⤶
for i = 1, #str do⤶
surface.SetTextColor( HSVToColor( i * frequency % 360, 1, 1 ) )
local w = surface.GetTextSize( string.sub( str, 1, i - 1 ) )
surface.SetTextPos( x + w, y )
surface.DrawText( string.sub( str, i, i ) )
end
⤶
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 )⤶
</code>
<output><image src="DrawRainbowText.png" alt="300px"/></output>
</example>
<example>
<description>A helper function for printing rainbow text in the chat.</description>
<code>
local function ChatPrintRainbow( frequency, str )
local text = {}⤶
⤶
for i = 1, #str do⤶
local function PrintRainbowText( frequency, str )
⤶
local text = {}
⤶
for i = 1, #str do⤶
table.insert( text, HSVToColor( i * frequency % 360, 1, 1 ) )
table.insert( text, string.sub( str, i, i ) )
end
chat.AddText( unpack( text ) )⤶
MsgC( unpack( text ) )
⤶
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!" )⤶
</code>
<output><image src="ChatPrintRainbow.png" alt="300px"/>
<image src="ConsolePrintRainbow.png" alt="300px"/></output>
</example>