Im trying to generate a color gradient using ColdFusion. My current code below works but it basically will only display the start color till the very last line then it will show the end color. No gradient. Just a big block of red then one line of blue. What am i not seeing wrong with my code?
<cfset BoxNumber = RandRange(100,1000) >
<cfset Start_Red = 255 >
<cfset Start_Green = 0 >
<cfset Start_Blue = 0 >
<cfset End_Red = 0 >
<cfset End_Green = 0 >
<cfset End_Blue = 255 >
<div id="color-band">
<cfloop index = "i" from = 0 to = #BoxNumber# >
<cfset Percent = i \ (BoxNumber - 1) >
<cfset Red = int(((End_Red - Start_Red) * Percent) + Start_Red) >
<cfset Green = int(((End_Green - Start_Green) * Percent) + Start_Green) >
<cfset Blue = int(((End_Blue - Start_Blue) * Percent) + Start_Blue) >
<div style="background-color:rgb(<cfoutput>#Red#</cfoutput>, <cfoutput>#Green# </cfoutput>, <cfoutput>#Blue#</cfoutput>)"> </div>
</cfloop>
</div>
I do know a ColdFusion server can be hard to come by sometimes so i can test the code if need be.
At least one problem is the
percentformula returns zero (0) most of the time. So the colors never increment because the code is just adding0to the starting value. I think the color formula is off as well.Unless this is a learning exercise, you might want to look for an existing function or method rather than reinventing the wheel. (Edit: For example, take a look at Craig’s suggestion). Otherwise, you might take a look at cflib.org for inspiration. The fadeList function shows the basic concept for generating a gradient (albeit with hex). This is not exactly right, but something along these lines