0
\$\begingroup\$

I wanted to generate a 7 segment display whose color brightness varies with time, this can be done by just using linear relationship.

Color activeColour2 = new Color(1, DateTime.Now.Millisecond*0.001f, DateTime.Now.Millisecond*0.001f);

So in 1 complete second, it produce color in the range of (1,0,0) to (1,1,1) as in the code below:

using System;
using UnityEngine;

public class ClockDigit2 : MonoBehaviour
{    
    static readonly bool[,] SEGMENT_IS_ACTIVE = new bool[,] {

        {true,  true,  true,  true,  true,  true,  false}, // 0
        {false, true,  true,  false, false, false, false}, // 1
        {true,  true,  false, true,  true,  false, true }, // 2
        {true,  true,  true,  true,  false, false, true }, // 3
        {false, true,  true,  false, false, true,  true }, // 4
        {true,  false, true,  true,  false, true,  true }, // 5
        {true,  false, true,  true,  true,  true,  true }, // 6
        {true,  true,  true,  false, false, false, false}, // 7
        {true,  true,  true,  true,  true,  true,  true }, // 8
        {true,  true,  true,  true,  false, true,  true }  // 9
    };

    
    public Color32 inactiveColour = Color.black;  


    public SpriteRenderer[] segments = new SpriteRenderer[7];
   

    public void Display(int number)
    {   
        Color activeColour2 = new Color(1, DateTime.Now.Millisecond*0.001f, DateTime.Now.Millisecond*0.001f);
        var digit = number % 10;
        if (digit < 0) digit *= -1;   

        for (int i = 0; i < 7; i++)
        {
            if (SEGMENT_IS_ACTIVE[digit, i])
            {
                segments[i].color = activeColour2;
            }
            else
            {
                segments[i].color = inactiveColour;
            }
        }  

    }    

    void Update()
    {    
        Display(DateTime.Now.Second);    
    }    
}

enter image description here

enter image description here

enter image description here

enter image description here

However, now I want to interpolate through all the colors in the circle above over 1 second.

I don't know how to express this as a formula for R, G, and B. The RGB value is not linear relationship: as you spin the selected color in the chart, you can notice the RGBA components vary together in a nonlinear form.

I considered using Lagrangian interpolation. However, this will not be accurate or might fail greatly because if you take a reference point at the circle and assume in 1sec, it made a complete revolution, you will notice there is a certain period of time where the RGB value is constant along the path: either 0 and 1.

So is there any method to get a formula that perfectly fits the color chart with respect to time?

\$\endgroup\$
6
  • \$\begingroup\$ Presumably you considered using Color.HSVToRGB to express your colour varying formula in terms of hue, saturation, and value? \$\endgroup\$
    – DMGregory
    Commented Feb 9, 2021 at 18:08
  • \$\begingroup\$ I check on website but not very sure how does that work? it looks unity has it own conversion formulae docs.unity3d.com/ScriptReference/Color.HSVToRGB.html which is different from the normal conversion rapidtables.com/convert/color/hsv-to-rgb.html? \$\endgroup\$
    – chuackt
    Commented Feb 9, 2021 at 18:21
  • \$\begingroup\$ What specific difference do you perceive? Where have you encountered an obstacle in adapting your code to adapt for that difference? \$\endgroup\$
    – DMGregory
    Commented Feb 9, 2021 at 18:23
  • \$\begingroup\$ thanks, using Color activeColour2 = Color.HSVToRGB(DateTime.Now.Millisecond*0.001f,1,1) got it working. I see, it using 1 to represent 360degree and 100%. Couldn't get it on the 1st glance. \$\endgroup\$
    – chuackt
    Commented Feb 9, 2021 at 18:32
  • \$\begingroup\$ If you solved your problem, want to post your solution as an answer? Also, I highly recommend spending a little more time reading the docs before asking in future - common operations like this very often have built-in API methods you can use if you look for them. \$\endgroup\$
    – DMGregory
    Commented Feb 9, 2021 at 18:36

1 Answer 1

0
\$\begingroup\$

I'm not aware of the Color.HSVToRGB method in Unity. using Color activeColour2 = Color.HSVToRGB(DateTime.Now.Millisecond*0.001f,1,1) give the desired result since the conversion is a linear relationship with respect to time.

\$\endgroup\$
1
  • \$\begingroup\$ If this solved your problem, don't forget to click the checkmark to mark this answer as "Accepted". \$\endgroup\$
    – DMGregory
    Commented Mar 11, 2021 at 20:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .