/    Sign up×
Community /Pin to ProfileBookmark

Help with looping

Hi all

JS newbie here. I need help with looping through an array I have added to a script.

My array is called ‘Colours’ and contains 7 RGB values. The idea of the script is to change the background colour by fading the colours in the array into one another but I need it to be contiuous – thus the request for help!

Here is the code. Any help will be greatly appreciated.

[CODE]
<script language=”Javascript”>

var Colours = new Array()
Colours[0] = “212,180,135,146,147,149”
Colours[1] = “146,147,149,96,90,168”
Colours[2] = “96,90,168,100,185,227”
Colours[3] = “100,185,227,230,65,141”
Colours[4] = “230,65,141,247,172,4”
Colours[5] = “247,172,4,96,90,168”
Colours[6] = “96,90,168,176,202,53”

function StringArray (n) {
this.length = n;
for (var i =1; i <= n; i++) {
this[i] = ‘ ‘
}
}

fader = new StringArray(16)

for(var i = 0; i < 10; i++) {
fader[i] = i
}

fader[10] = “a”
fader[11] = “b”
fader[12] = “c”
fader[13] = “d”
fader[14] = “e”
fader[15] = “f”

function hexadecimal(i) {
return (“” + fader[Math.floor(i/16)] + fader[i%16])
}

function fade(r2,g2,b2,r1,g1,b1) {
for(var i = 0; i <= speed; i++) {
var r = hexadecimal(Math.floor(r2 * ((speed-i)/speed) + r1 * (i/speed)))
var g = hexadecimal(Math.floor(g2 * ((speed-i)/speed) + g1 * (i/speed)))
var b = hexadecimal(Math.floor(b2 * ((speed-i)/speed) + b1 * (i/speed)))
document.bgColor = “#” + r + g + b
}
}

</script>

[/CODE]

When its not in a loop, the colours are called as such:

[CODE]
<script language=”Javascript”>
fade(212,180,135,146,147,149);
fade(146,147,149,96,90,168);
fade(96,90,168,100,185,227);
fade(100,185,227,230,65,141);
fade(230,65,141,247,172,4);
fade(247,172,4,96,90,168);
fade(96,90,168,176,202,53);
fade(212,180,135,146,147,149);
fade(146,147,149,96,90,168);
fade(96,90,168,100,185,227);
fade(100,185,227,230,65,141);
fade(230,65,141,247,172,4);
fade(247,172,4,96,90,168);
fade(96,90,168,176,202,53);
</script>

[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorNov 29.2007 — have you considered using CSS "on-the-fly" style (element.style.backgroundColor) and with that, straight the RGB colors instead oh Hexa?

What do you mean by continuous? Repeating the change? setInterval() or setTimeout() methods will help you to do that.
Copy linkTweet thisAlerts:
@kbc1authorNov 30.2007 — Thanks foe the reply.

Would you be able to provide an example of what you mean so I can follow it?
×

Success!

Help @kbc1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...