- Home /
HSV to RGB
I am trying to have the camera background color cycle over time by changing the Hue value each frame. I found a script for HSVToRGB but it isn't quite working as the background just shows up black no matter what values i put in.
#pragma strict
var cam : Camera;
var Hcol : float;
var Scol : float;
var Vcol : float;
function Start ()
{
}
function FixedUpdate ()
{
cam.backgroundColor = HSVtoRGB(Hcol,Scol,Vcol);
}
function HSVtoRGB(h : float, s : float, v : float)
{
h=h%1;
s=s%1;
v=v%1;
var r : float;
var g : float;
var b : float;
var i : float;
var f : float;
var p : float;
var q : float;
var t : float;
i = Mathf.Floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6)
{
case 0: r = v; g = t; b = p; break;
case 1: r = q; g = v; b = p; break;
case 2: r = p; g = v; b = t; break;
case 3: r = p; g = q; b = v; break;
case 4: r = t; g = p; b = v; break;
case 5: r = v; g = p; b = q; break;
}
return Color(r,g,b);
}
No errors appear when i run the game, the values for Hue, Saturation, and Value just all go to zero making the background black. I would like Hue to be zero but saturation to be 150 and value to be 90.
Thank you for any help
Are you using 0-1 for h s and v? If you read through the code, that's what it seems to want. Note how various cases just copy h, s and/or v directly into rgb (which are 0-1 in Unity.)
(And the first three lines are nonsense. They seem to be "fixing" non-0-1 values in a really bad way. I'd just delete them.)
I got rid of those lines and changed the values i was using then i set it up so hue would increase over time and it works great thanks for the help
Answer by greggman · May 07, 2015 at 07:07 PM
I'd say the first 3 lines should be
h=h >= 0 ? h % 1 : 1 - (-h % 1);
s=Mathf.Max(0, Mathf.Min(1, s));
v=Mathf.Max(0, Mathf.Max(1,v));
These lines let you safely adjust h, s, and v without worrying about them going out of range. Kind of like Mathf.sin doesn't go out of range.
Otherwise my guess is the switch isn't happy using a float?
Try changing i to an int
var i : int;
Your answer
Follow this Question
Related Questions
Change the background color attribute of a camera in C#? 2 Answers
How can I create a gradient color background for my game, without using skybox? 2 Answers
A way to set a custom camera.viewport background color? 1 Answer
Randomly Transition Camera Background Color? 2 Answers
How to randomly chenge camerabackground color every 5 seconds? 1 Answer