Algorithm Question: need dynamically INCREMENT from 00FF00 to FF0000 via TIME, C # / Java

I want to change the color of Bright Green to Dark Red with time (240 hours). The best way I see is to change the hexadecimal combo from 00FF00 to FF0000.

I do not know how to dynamically count to FF0000 from 00FF00 for the life of me. I am looking at a 10-day period, so most likely more than 240 hours to increase.

Can anyone help me out?

I never took a class of algorithms, so I think this may have something to do with this problem.

If you have a better way to do this, let me know.

I'm looking for some kind of code here. Thank you, guys. It can be in any language, but will inevitably be converted to C #.

+5
source share
5 answers

Think about it in terms of components. Despite the fact that it looks like one large hexadecimal number, it is actually three side by side.

At the beginning, red is 0, green is 255 (FF), blue is 0.
At the end, red is 255, green is 0, blue is 0.

So each (the amount of time you have / 255), increase red by 1 and decrease green by 1.

+8
source

, , HSV, RGB. HSV , - , . RGB- , .

- RGB FF0000 00FF00. - 7f7f00, -.

HSV. FF0000 00FF00 (), , - ffff00.

- RGB, , - B4B400, (B4 hex = 180 dec = 255/sqrt (2)), - . , RGB, , . , , , HSV- , 100%.

Imageshack


Java, HSB, - HSB , , RGB, , h, s, v:

static Color hsvInterpolate ( float mix, Color c0, Color c1 ) {
    float[] hsv0 = new float[3];
    float[] hsv1 = new float[3];

    float alt = 1.0f - mix;

    Color.RGBtoHSB( c0.getRed(), c0.getGreen(), c0.getBlue(), hsv0 );
    Color.RGBtoHSB( c1.getRed(), c1.getGreen(), c1.getBlue(), hsv1 );

    float h = mix * hsv0 [ 0 ] +  alt * hsv1 [ 0 ];
    float s = mix * hsv0 [ 1 ] +  alt * hsv1 [ 1 ];
    float v = mix * hsv0 [ 2 ] +  alt * hsv1 [ 2 ];

    return Color.getHSBColor ( h, s, v );
}

, # , .

static Color vectorInterpolate ( float mix, Color c0, Color c1 ) {
    float alt = 1.0f - mix;

    double x0 = c0.getRed();
    double y0 = c0.getGreen();
    double z0 = c0.getBlue();

    double x1 = c1.getRed();
    double y1 = c1.getGreen();
    double z1 = c1.getBlue();

    double mag0 = sqrt( x0*x0 + y0*y0 + z0*z0 );
    double mag1 = sqrt( x1*x1 + y1*y1 + z1*z1 );

    double x = mix * x0 + alt * x1;
    double y = mix * y0 + alt * y1;
    double z = mix * z0 + alt * z1;

    double mag  = mix * mag0 + alt * mag1;
    double scale = mag / sqrt( x*x + y*y + z*z );

    return new Color ( 
        clamp ( x * scale ),
        clamp ( y * scale ),
        clamp ( z * scale ) );
}

static int clamp ( double value ) {
    int x = (int) round ( value );

    if ( x > 255 ) return 255;
    if ( x < 0 ) return 0;
    return x;
}

, RGB, , .


HSY, , Dave Green cube helix color .

+29

time_remaining ( 0 239)

green = 255 * (time_remaining/239)
= 255 -
= 0

color = (, , )

+3
source

here the quick answer to java from red to green (for example, you can change it) the value is the current valuein time, and allis the sum of the time ...

public static String progressiveColor(int value, int all){

    int red = 255 - (int)((float)(value*255)/(float)all);
    int green = (int)((float)(value*255)/(float)all);
    return String.format("#%06X", (0xFFFFFF & Color.argb(255, red, green, 0)));

}
0
source

Here is the Pete Java code translated into C # in case anyone is looking for it. It works great for my purposes (from black to dark red and vice versa).

    static Color VectorInterpolate(float mix, Color c0, Color c1)
    {
        float alt = 1.0f - mix;

        double x0 = c0.R;
        double y0 = c0.G;
        double z0 = c0.B;

        double x1 = c1.R;
        double y1 = c1.G;
        double z1 = c1.B;

        double mag0 = Math.Sqrt(x0 * x0 + y0 * y0 + z0 * z0);
        double mag1 = Math.Sqrt(x1 * x1 + y1 * y1 + z1 * z1);

        double x = mix * x0 + alt * x1;
        double y = mix * y0 + alt * y1;
        double z = mix * z0 + alt * z1;

        double mag = mix * mag0 + alt * mag1;
        double scale = mag / Math.Sqrt(x * x + y * y + z * z);

        return Color.FromRgb(Clamp(x * scale), Clamp(y * scale), Clamp(z * scale));
    }

    static byte Clamp(double value)
    {
        var x = (int)Math.Round(value);

        if (x > 255) return 255;
        if (x < 0) return 0;
        return (byte) x;
    }
0
source

All Articles