Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
More Jobs
Recent
Cheap ruby block templating
Bindings on caller
Initial script execution very slow and skewing results
suppress ERB newlines
Keeping the database clean
Arrays, loops, strings...
Similar virtual attributes and their getters/setter
Check type is XML Serializable
Serena Collage slideshow
Shortest regular expression for matching a subdomain.
Popular
Caching Methods
How to find max, min of three numbers?
Similar virtual attributes and their getters/setter
Shortest regular expression for matching a subdomain.
Arrays, loops, strings...
Cheap ruby block templating
Check type is XML Serializable
Bindings on caller
Apache log file sorting
Tab-Switching in jQuery
Pastable version of
posterize
<div style="overflow:auto;border:solid 1px #ccc;background:#000;color:#F8F8F8"> <div class="section"> <pre style="float:left;margin:0 10px;border-right:0;color:#666;">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85</pre> <pre class="sunburst"><span style="color:#99CF50;">def</span> <span style="color:#89BDFF;">montage</span>(): topLeft <span style="color:#E28964;">=</span> makePicture(getMediaPath(<span style="color:#65B042;"><span style="color:#65B042;">"</span>images-1.jpg<span style="color:#65B042;">"</span></span>)) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> Top left picture, type in later</span> topRight <span style="color:#E28964;">=</span> makePicture(getMediaPath(<span style="color:#65B042;"><span style="color:#65B042;">"</span>images-2.jpg<span style="color:#65B042;">"</span></span>)) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> Top Right picture, </span> bottomLeft <span style="color:#E28964;">=</span> makePicture(getMediaPath(<span style="color:#65B042;"><span style="color:#65B042;">"</span>images-3.jpg<span style="color:#65B042;">"</span></span>)) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> Bottom Left picture</span> bottomRight <span style="color:#E28964;">=</span> makePicture(getMediaPath(<span style="color:#65B042;"><span style="color:#65B042;">"</span>images-4.jpg<span style="color:#65B042;">"</span></span>)) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> Bottom right picture</span> middlePicture <span style="color:#E28964;">=</span> makePicture(getMediaPath(<span style="color:#65B042;"><span style="color:#65B042;">"</span>images.jpg<span style="color:#65B042;">"</span></span>)) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> Middle picture</span> width <span style="color:#E28964;">=</span> getWidth(topLeft) height <span style="color:#E28964;">=</span> getHeight(topLeft) canvas <span style="color:#E28964;">=</span> makeEmptyPicture(width<span style="color:#E28964;">*</span><span style="color:#3387CC;">3</span>, height<span style="color:#E28964;">*</span><span style="color:#3387CC;">3</span>) left <span style="color:#E28964;">=</span> <span style="color:#3387CC;">1</span> top <span style="color:#E28964;">=</span> <span style="color:#3387CC;">1</span> midX <span style="color:#E28964;">=</span> width <span style="color:#E28964;">+</span><span style="color:#3387CC;">1</span> midY <span style="color:#E28964;">=</span> height <span style="color:#E28964;">+</span><span style="color:#3387CC;">1</span> <span style="color:#99CF50;">def</span> <span style="color:#89BDFF;">posterize</span>(<span style="color:#3E87E3;">pic</span>): <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> simplifies the color range</span> <span style="color:#E28964;">for</span> pixel <span style="color:#E28964;">in</span> getPixels(pic): <span style="color:#E28964;">if</span> getRed(pixel) <span style="color:#E28964;">></span> <span style="color:#3387CC;">127</span>: redness <span style="color:#E28964;">=</span> <span style="color:#3387CC;">255</span> <span style="color:#E28964;">else</span>: redness <span style="color:#E28964;">=</span> <span style="color:#3387CC;">0</span> <span style="color:#E28964;">if</span> getGreen(pixel) <span style="color:#E28964;">></span> <span style="color:#3387CC;">127</span>: greenness <span style="color:#E28964;">=</span> <span style="color:#3387CC;">255</span> <span style="color:#E28964;">else</span>: greenness <span style="color:#E28964;">=</span> <span style="color:#3387CC;">0</span> <span style="color:#E28964;">if</span> getBlue(pixel) <span style="color:#E28964;">></span> <span style="color:#3387CC;">127</span>: blueness <span style="color:#E28964;">=</span> <span style="color:#3387CC;">255</span> <span style="color:#E28964;">else</span>: blueness <span style="color:#E28964;">=</span> <span style="color:#3387CC;">0</span> setRed(pixel, redness) setGreen(pixel, greenness) setBlue(pixel, blueness) <span style="color:#E28964;">return</span> pic <span style="color:#99CF50;">def</span> <span style="color:#89BDFF;">grayscale</span>(<span style="color:#3E87E3;">gPic</span>): <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>sets RGB values equal to one another</span> <span style="color:#E28964;">for</span> p <span style="color:#E28964;">in</span> getPixels(gPic): intensity <span style="color:#E28964;">=</span> (getRed(p) <span style="color:#E28964;">+</span> getGreen(p) <span style="color:#E28964;">+</span> getBlue(p))<span style="color:#E28964;">/</span><span style="color:#3387CC;">3</span> setColor(p, makeColor(intensity, intensity, intensity)) <span style="color:#E28964;">return</span> gPic <span style="color:#99CF50;">def</span> <span style="color:#89BDFF;">negative</span>(<span style="color:#3E87E3;">nPic</span>): <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> makes all the colors opposite original values</span> <span style="color:#E28964;">for</span> px <span style="color:#E28964;">in</span> getPixels(nPic): red1 <span style="color:#E28964;">=</span> getRed(px) green1 <span style="color:#E28964;">=</span> getGreen(px) blue1 <span style="color:#E28964;">=</span> getBlue(px) negColor <span style="color:#E28964;">=</span> makeColor(<span style="color:#3387CC;">255</span><span style="color:#E28964;">-</span>red1, <span style="color:#3387CC;">255</span><span style="color:#E28964;">-</span>green1, <span style="color:#3387CC;">255</span><span style="color:#E28964;">-</span>blue1) setColor(px, negColor) <span style="color:#E28964;">return</span> nPic <span style="color:#99CF50;">def</span> <span style="color:#89BDFF;">sunset</span>(<span style="color:#3E87E3;">sPic</span>): <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> decreases blue and green to add a warmer red tone</span> <span style="color:#E28964;">for</span> p <span style="color:#E28964;">in</span> getPixels(sPic): value <span style="color:#E28964;">=</span> getBlue(p) setBlue(p, value<span style="color:#E28964;">*</span><span style="color:#3387CC;">0.07</span>) value1 <span style="color:#E28964;">=</span> getGreen(p) setGreen(p, value1<span style="color:#E28964;">*</span><span style="color:#3387CC;">0.07</span>) <span style="color:#E28964;">return</span> sPic <span style="color:#99CF50;">def</span> <span style="color:#89BDFF;">copyPicture</span>(<span style="color:#3E87E3;">littlePic</span>, <span style="color:#3E87E3;">bg</span>, <span style="color:#3E87E3;">x</span>, <span style="color:#3E87E3;">y</span>): <span style="color:#E28964;">for</span> destX <span style="color:#E28964;">in</span> <span style="color:#DAD085;">range</span>(getWidth(littlePic)): <span style="color:#E28964;">for</span> destY <span style="color:#E28964;">in</span> <span style="color:#DAD085;">range</span>(getHeight(littlePic)): fromPixel <span style="color:#E28964;">=</span> getPixel(littlePic, destX<span style="color:#E28964;">+</span><span style="color:#3387CC;">1</span>, destY<span style="color:#E28964;">+</span><span style="color:#3387CC;">1</span>) destPixel <span style="color:#E28964;">=</span> getPixel(bg, destX<span style="color:#E28964;">+</span>x, destY<span style="color:#E28964;">+</span>y) setColor(destPixel, getColor(fromPixel)) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>posterized pic top left</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>posterizedTL = posterize(TopLeft)</span> copyPicture(posterize(topLeft), canvas, left, top) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>grayscale pic top right</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>grayscaleTR = Grayscale(topRight)</span> copyPicture(grayscale(topRight), canvas, midX, top) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>negative pic bottom left</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>negativeBL = negative(bottomLeft)</span> copyPicture(negative(bottomLeft), canvas, left, midY) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>sunset pic bottom right</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>sunsetBR = sunset(bottomRight)</span> copyPicture(sunset(bottomRight), canvas, midX, midY) <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span> "original' pic</span> copyPicture(middlePicture, canvas, midX<span style="color:#E28964;">/</span><span style="color:#3387CC;">2</span>, midY<span style="color:#E28964;">/</span><span style="color:#3387CC;">2</span>) <span style="color:#E28964;">return</span> canvas </pre> </div> </div> <a href="http://refactormycode.com/codes/28-posterize" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://refactormycode.com/images/small_logo.gif" style="border:0" /></a>