{"id":57,"date":"2007-05-30T12:24:59","date_gmt":"2007-05-30T17:24:59","guid":{"rendered":"http:\/\/www.pixelwit.com\/blog\/2007\/05\/30\/random-candy-colors\/"},"modified":"2009-08-13T17:09:24","modified_gmt":"2009-08-13T22:09:24","slug":"random-candy-colors","status":"publish","type":"post","link":"https:\/\/www.pixelwit.com\/blog\/2007\/05\/30\/random-candy-colors\/","title":{"rendered":"Random Candy Colors"},"content":{"rendered":"<p>If you saw my recent &#8220;<a title=\"Brightly Colored Flash Art\" href=\"https:\/\/www.pixelwit.com\/blog\/2007\/05\/candy-cosmos\/\">Candy Cosmos<\/a>&#8221; post, you may have noticed that the randomly generated colors tend to be rather &#8220;vibrant&#8221;.<\/p>\n<p><img src='https:\/\/www.pixelwit.com\/blog\/wp-content\/uploads\/2007\/05\/rainbbowgradient.jpg' title='Brightly Colored Banded Gradient' alt='rainbbowgradient.jpg' \/><\/p>\n<p>Pure red, green, blue, cyan, magenta and yellow colors appear frequently, while pure black and white colors occur occasionally, and colors near 50% gray tend to be the least common.   The bright candy colors are a side effect caused by<!--more--> <a title=\"How to Limit a Number Between 2 Values\" href=\"https:\/\/www.pixelwit.com\/blog\/2007\/05\/how-to-limit-a-number-between-2-values\/\">&#8220;limiting&#8221; the random values<\/a> generated for each color channel.<\/p>\n<p>Let&#8217;s say we start off with a neutral gray color with (R, G, B) values of (100, 100, 100).  To get our next color we generate a random number between 5 and 10 then we either add it to, or subtract it from, the red channel.  That means that there is a 50% chance our new red channel value will be between 90 and 95 and a 50% chance that it will be between 105 and 110.  We repeat this process for each of the three color channels then combine them to make our new color.  This produces a color similar to our original color, yet noticeably different.<\/p>\n<p>The method above works well until the values of the color channels approach 0 or 255.  Suppose the red channel has a value of 0.  Using the above randomization technique would generate potential values between (-10 and -5) or between (5 and 10).  Color channels can&#8217;t hold values below 0 or above 255 so corrective actions need to be taken.  A simple solution is to &#8220;limit&#8221; the values.  If the number is below 0 it is set to 0.  If the number is above 255 it is set to 255.  That means that once a color channel reaches a value of 0 or 255 there is a 50% chance that the value will remain unchanged.  This is because there is a 50% chance that the next random number will either be added to, or subtracted from, the original channel value and thus produce a value outside the acceptable range which in turn results in no change to the original value.<\/p>\n<p>Since the color channel values tend to hover near 0 (0x00) and 255 (0xFF), pure CMY colors (0xFFFF00, 0xFF00FF, 0x00FFFF) or pure RGB colors (0xFF0000, 0x00FF00, 0x0000FF) are common.  Here is some code to illustrate:<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:100%;height:700px;\"><div class=\"actionscript codecolorer\"><span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Create an object to contain our 3 color channels (r, g, b).<\/span><br \/>\n<span class=\"kw2\">var<\/span> myColorObj = <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Create the RGB channels and assign values to each of them.<\/span><br \/>\nmyColorObj.<span class=\"me1\">r<\/span> = <span class=\"nu0\">100<\/span>;<br \/>\nmyColorObj.<span class=\"me1\">g<\/span> = <span class=\"nu0\">100<\/span>;<br \/>\nmyColorObj.<span class=\"me1\">b<\/span> = <span class=\"nu0\">100<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Each color channel will be randomized.<\/span><br \/>\n<span class=\"co1\">\/\/ The color channel will go up or down by at least the MIN value.<\/span><br \/>\n<span class=\"kw2\">var<\/span> minColorChannelVary = <span class=\"nu0\">5<\/span>;<br \/>\n<span class=\"co1\">\/\/ The color channel will go up or down no more than the MAX value.<\/span><br \/>\n<span class=\"kw2\">var<\/span> maxColorChannelVary = <span class=\"nu0\">10<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Goes through each color channel in a color object<\/span><br \/>\n<span class=\"co1\">\/\/ and adds or subtracts a random number<\/span><br \/>\n<span class=\"co1\">\/\/ between the min and max values<\/span><br \/>\n<span class=\"co1\">\/\/ to each color channel.<\/span><br \/>\n<span class=\"co1\">\/\/ Color channel values are limited between 0 and 255.<\/span><br \/>\n<span class=\"kw2\">function<\/span> randomizeColor<span class=\"br0\">&#40;<\/span>colorObj, <span class=\"kw3\">min<\/span>, <span class=\"kw3\">max<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">for<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw2\">var<\/span> i <span class=\"kw1\">in<\/span> colorObj<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> chnl = colorObj<span class=\"br0\">&#91;<\/span>i<span class=\"br0\">&#93;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> ran = <span class=\"kw3\">min<\/span> + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">random<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> <span class=\"br0\">&#40;<\/span>max-<span class=\"kw3\">min<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; chnl += <span class=\"br0\">&#40;<\/span><span class=\"kw3\">Math<\/span>.<span class=\"kw3\">round<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw3\">Math<\/span>.<span class=\"kw3\">random<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> ? ran : -ran<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; colorObj<span class=\"br0\">&#91;<\/span>i<span class=\"br0\">&#93;<\/span> = <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">max<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">0<\/span>, <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">min<\/span><span class=\"br0\">&#40;<\/span>chnl, <span class=\"nu0\">255<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> colorObj;<br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Converts 3 color channels in the color object to<\/span><br \/>\n<span class=\"co1\">\/\/ one hexadecimal value like 0xFFFFFF.<\/span><br \/>\n<span class=\"kw2\">function<\/span> rgbToHex <span class=\"br0\">&#40;<\/span>colorObj<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span><span class=\"br0\">&#40;<\/span>colorObj.<span class=\"me1\">r<\/span><span class=\"sy0\">&lt;&lt;<\/span><span class=\"nu0\">16<\/span> <span class=\"sy0\">|<\/span> colorObj.<span class=\"me1\">g<\/span><span class=\"sy0\">&lt;&lt;<\/span><span class=\"nu0\">8<\/span> <span class=\"sy0\">|<\/span> colorObj.<span class=\"me1\">b<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Draws a 1 by 100 box using the specified color.<\/span><br \/>\n<span class=\"kw2\">function<\/span> drawBox<span class=\"br0\">&#40;<\/span>x, <span class=\"kw3\">color<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/clear();<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">beginFill<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw3\">color<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">moveTo<\/span><span class=\"br0\">&#40;<\/span>x, <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">lineTo<\/span><span class=\"br0\">&#40;<\/span>x+<span class=\"nu0\">1<\/span>, <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">lineTo<\/span><span class=\"br0\">&#40;<\/span>x+<span class=\"nu0\">1<\/span>, <span class=\"nu0\">100<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">lineTo<\/span><span class=\"br0\">&#40;<\/span>x, <span class=\"nu0\">100<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">lineTo<\/span><span class=\"br0\">&#40;<\/span>x, <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Draw a random color and then move over to the right 1 pixel<\/span><br \/>\n<span class=\"co1\">\/\/ and draw the next random color. &nbsp;Repeat 500 times until<\/span><br \/>\n<span class=\"co1\">\/\/ a brightly colored banded gradient covers the stage.<\/span><br \/>\n<span class=\"kw2\">function<\/span> genCandyGradient<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">clear<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">for<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw2\">var<\/span> i=<span class=\"nu0\">0<\/span>; i<span class=\"sy0\">&lt;<\/span><span class=\"nu0\">500<\/span>; i++<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; randomizeColor<span class=\"br0\">&#40;<\/span>myColorObj, minColorChannelVary, maxColorChannelVary<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; drawBox<span class=\"br0\">&#40;<\/span>i, rgbToHex <span class=\"br0\">&#40;<\/span>myColorObj<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\ngenCandyGradient<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Draw a new gradient every time the mouse is pressed.<\/span><br \/>\n<span class=\"kw3\">onMouseDown<\/span> = genCandyGradient;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p><\/code><\/p>\n<p>Here is the SWF produced by the above code, click it to see new random colors:<\/p>\n\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n\t\t\tid=\"fm_randcandycolor_02_1358558451\"\n\t\t\tclass=\"flashmovie\"\n\t\t\twidth=\"500\"\n\t\t\theight=\"100\">\n\t<param name=\"movie\" value=\"https:\/\/www.pixelwit.com\/blog\/wp-content\/uploads\/2007\/05\/randcandycolor_02.swf\" \/>\n\t<!--[if !IE]>-->\n\t<object\ttype=\"application\/x-shockwave-flash\"\n\t\t\tdata=\"https:\/\/www.pixelwit.com\/blog\/wp-content\/uploads\/2007\/05\/randcandycolor_02.swf\"\n\t\t\tname=\"fm_randcandycolor_02_1358558451\"\n\t\t\twidth=\"500\"\n\t\t\theight=\"100\">\n\t<!--<![endif]-->\n\t\t\n\t<!--[if !IE]>-->\n\t<\/object>\n\t<!--<![endif]-->\n<\/object>\n<p>I doubt you'll need to create a brightly colored banded gradient every day but it may prove useful some time.<\/p>\n<p>So now you know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A novel Flash ActionScript function which generates a series of similar yet slightly different colors resulting in a brightly colored banded gradient.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[8],"tags":[23,46,36,37],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-flash","tag-actionscript","tag-color","tag-swfs","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":0,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}