1

I am trying to implement a piece of code that works in java, the code takes RGB and fades into the next color, based on an array, and changes taht RGB into hex for javascript to understand it. (I did change arrays and variables to be compatible with javascript)... and yet, it still doesn't seem to work. I've attempted to troubleshoot and find a solution, but couldn't find any. The problem I've been noticing, is the blue seems to be skipped over, and the green is changed instead. I think the setpoint is somehow getting set to a negative number, though I don't know where and why. Thanks for the help (sorry if both the code and this brick of text is a little long)

var curColorHex;
var targetRGBVals = [];
var rgbIncrement;
var nextRainbowVal = [];
    nextRainbowVal[0]=255;
    nextRainbowVal[1]=0;
    nextRainbowVal[2]=0;//RED

    nextRainbowVal[3]=255;
    nextRainbowVal[4]=165;
    nextRainbowVal[5]=0;//Orange

    nextRainbowVal[6]=255;
    nextRainbowVal[7]=255;
    nextRainbowVal[8]=0;//Yellow

    nextRainbowVal[9]=0;
    nextRainbowVal[10]=255
    nextRainbowVal[11]=0;//Green

    nextRainbowVal[12]=0;
    nextRainbowVal[13]=0;
    nextRainbowVal[14]=255;//Blue

    nextRainbowVal[13]=111;
    nextRainbowVal[14]=0;
    nextRainbowVal[15]=255;//Indigo

    nextRainbowVal[16]=143;
    nextRainbowVal[17]=0;
    nextRainbowVal[18]=255;//Violet

var rgbVals = [];
var curColor = [];
var colorIndex;
var equal = [];

function onPageLoad(){//this is only code specific to my problem, none of the other code
    colorIndex=0;
    equal[0]=true;
    equal[1]=true;
    equal[2]=true;

    rgbIncrement=1;//+1 so increment can't equal 0
    curColor[0]=nextRainbowVal[0];
    curColor[1]=nextRainbowVal[1];
    curColor[2]=nextRainbowVal[2];

    setInterval("backgroundChange();",10);

}//onPageLoad

function backgroundChange(){//all for loops are set to 3 because RGB never goes above 3 different variables (less chance of error)
    for(var i=0;i<3;i++){
    rgbVals[i]=curColor[i];
    }//for

    targetRGBVals[0] = nextRainbowVal[colorIndex];//Initializes the targetRGB val
    targetRGBVals[1] = nextRainbowVal[colorIndex+1];
    targetRGBVals[2] = nextRainbowVal[colorIndex+2];

    for(var i=0; i<3; i++){//for loop to see if it has reached target
        if(targetRGBVals[i] != rgbVals[i]){
            equal[i] = false;
        }//if
        else equal[i]=true;
    }//for

    if(equal[0]&&equal[1]&&equal[2]){//changes setpoints after the RGB vals have reached their target
        if(colorIndex>(nextRainbowVal.length-3)){//this keeps setting the color index to next color when the curColor is at target, if it gets to the last color, it resets at beginning of array
            colorIndex = 0;
        }//if

        for(var g = 0; g<targetRGBVals.length; g++){//this sets the next target RGB vals
            targetRGBVals[g] = nextRainbowVal[colorIndex+g];
        }//for

        colorIndex += 3;
    }//if

    for(var m = 0; m<rgbVals.length; m++){//this loop adds/subtracts from RGB vals by the increment, and also checks if the color is within the amount of the increment (so it doesn't bounce back and forth between colors)                        
        if((rgbVals[m] != targetRGBVals[m])&& !equal[m]){
            if((rgbVals[m]>=(targetRGBVals[m] - rgbIncrement))&&(rgbVals[m]<=(targetRGBVals[m] + rgbIncrement))) rgbVals[m] = targetRGBVals[m];
            else rgbVals[m] += targetRGBVals[m] > rgbVals[m] ? +rgbIncrement : -rgbIncrement;
            if(rgbVals[m]<0) rgbVals[m]=0;
        }//if
    }//for

    curColor = rgbVals;
    console.log(curColor);
    console.log(rgbToHex(curColor[0],curColor[1],curColor[2]));
    hexColor = rgbToHex(curColor[0],curColor[1],curColor[2]);
    document.getElementById("bodyColor").style.backgroundColor=hexColor;
}//backgroundChange

function componentToHex(c){
    console.log(c);
    var hex = c.toString(16);
    return hex.length == 1 ? "0" + hex : hex;
}//componentToHex

function rgbToHex(r,g,b){
    return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}//rgbToHex
2
  • There is no need to convert your result to hex. Just use the format ...style.backgroundColor='rgb('+curColor[0]+','+curColor[1]+','+curColor[2]+')'; Commented Mar 2, 2013 at 1:36
  • i was attempting to use that beforehand, though javascript was giving me an error Commented Mar 2, 2013 at 1:38

1 Answer 1

2

Assuming this is nothing more than eye-candy, then browser support isn't a major issue. If that's the case, then the following will work in all except IE 9 and below (but it does work in IE 10):

CSS:

#bodyColor {
    animation: holyfuckrainbows 10s linear;
    -webkit-animation: holyfuckrainbows 10s linear;
}
@keyframes holyfuckrainbows {
    0% {background-color:#f00}
    16% {background-color:#f80}
    33% {background-color:#ff0}
    50% {background-color:#0f0}
    66% {background-color:#00f}
    83% {background-color:#70f}
    100% {background-color:#90f}
}
@-webkit-keyframes holyfuckrainbows {
    0% {background-color:#f00}
    16% {background-color:#f80}
    33% {background-color:#ff0}
    50% {background-color:#0f0}
    66% {background-color:#00f}
    83% {background-color:#70f}
    100% {background-color:#90f}
}

No JavaScript necessary. Adjust the 10s as needed to lengthen or shorten the duration of the rainbow.

If you need it to loop, you can do so by adding infinite after the 10s.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, though, where might the program be at fault for skipping the blue value, and changing green instead? (at this point i'm just curious in what is wrong)
and i just realized my problem... it's really awkward when you don't see that you messed up numbering your array correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.