String to hex, hex to rgba?

I have a (hopefully) simple question. In my script I have the hours, minutes, and secods displayed like this:
#145723 Currently this is just a multiple varialbes stringed together. I want to be able to convert the string, this is: hours + zerogap1 + minutes + zerogap2 + seconds the zerogap making it not ‘2’ but ‘02’ if the seconds/minutes are under 10, to a real hex value and then that hex value to an RGBA value. This RGBA value is going to set a materials color.

To make that easier to understand, I am recreating the Hex Clock in Unity: Hex clock

EDIT: is this possible in C#?

Thanks,
Dream

Im not sure I fully understand but maybe this?

Edit.

Also this would give you the string:
string.Format(“{0:X2}{1:X2}{2:X2}”, hours, minutes, seconds);

To make a number which is < 10 show as 2 digits:

According to the documentation it would appear like so:

int someInt = 7;
string result = someInt.ToString("D2");

Debug.Log(result);
// Should print "07"

Would ensure that the number always consists of at least 2 digits. 01 instead of 1 for example.

As for the rest Im not too sure what youre asking.

A quick look at that website’s source code shows:

function refreshData()
{
    x = 1;  // x = seconds
     var d = new Date()
     var h = d.getHours();
     var m = d.getMinutes();
     var s = d.getSeconds();
   
     if (h<=9) {h = '0'+h};
     if (m<=9) {m = '0'+m};
    if (s<=9) {s = '0'+s};
  
     var    color = '#'+h+m+s;
   
    $("div.background").css("background-color", color );
    $("p#hex").text(color);
   
    setTimeout(refreshData, x*1000);
}

This can be simplified using the DateTime class, and a string format parameter:

string color = "#" + DateTime.Now.ToString("HHmmss");
Debug.Log(color);
Color c;
ColorUtility.TryParseHtmlString(color, out c);
Camera.main.backgroundColor = c; // use the color
2 Likes

Thanks for the code!

It works perfectly! Thanks!

Dream.

1 Like

That works really well. Thanks!
Dream.

3 Likes