javascript - Limit canvas colors to a specific array -
how can limit rgb color space of canvas image specific array of colors? f.ex:
var colors = ['#aaffee','#cc44cc','#00cc55','#0000aa']; var rgb = [230,111,90]; // match rgb colors , return closest match
i need closest match array when loop through pixels in canvas image data. there clever function can that?
how limit colors specific color palette
you must map each , every original pixel nearest palette color.
to calculate distance between original , palette colors on colorwheel.
here illustration. original pixel (orange color) has arrows each color in our specified palette (assume our palette has 3 specified colors: red, green, blue).
the palette color shortest arrow length substituted original pixel.
since orange-red arrow shortest, palette red substituted original orange.
this important function maps original color palette color:
// use euclidian distance find closest color // send in rgb of pixel substituted function mapcolortopalette(red,green,blue){ var color,diffr,diffg,diffb,diffdistance,mappedcolor; var distance=25000; for(var i=0;i<palette.length;i++){ color=palette[i]; diffr=( color.r - red ); diffg=( color.g - green ); diffb=( color.b - blue ); diffdistance = diffr*diffr + diffg*diffg + diffb*diffb; if( diffdistance < distance ){ distance=diffdistance; mappedcolor=palette[i]; }; } return(mappedcolor); }
here code , fiddle: http://jsfiddle.net/m1erickson/gwqqh/
note: can improve code using hash tables, tree searches, etc.
<!doctype html> <html> <head> <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css --> <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> <style> body{ background-color: ivory; padding:15px; } canvas{border:1px solid red;} </style> <script> $(function(){ var canvasoriginal=document.getelementbyid("originalcanvas"); var ctx=canvasoriginal.getcontext("2d"); var canvasmapped=document.getelementbyid("mappedcanvas"); var ctxmapped=canvasmapped.getcontext("2d"); // draw off-colored rectangles ctx.beginpath(); ctx.fillstyle="rgb(140,70,60)"; //red-ish ctx.rect(10,10,20,20); ctx.fill(); ctx.beginpath(); ctx.fillstyle="rgb(70,140,60)"; //green-ish ctx.rect(10,40,20,20); ctx.fill(); ctx.beginpath(); ctx.fillstyle="rgb(70,60,140)"; //blue-ish ctx.rect(10,70,20,20); ctx.fill(); // create array of palette colors var palette=[{r:255,g:0,b:0},{r:0,g:255,b:0},{r:0,g:0,b:255}]; // load pixels array var imagedata=ctx.getimagedata(0,0,canvasoriginal.width,canvasoriginal.height); var data=imagedata.data; // rewrite pixels using mapped colors var mappedcolor; for(var i=0; i<data.length; i+=4) { mappedcolor = mapcolortopalette(data[i], data[i+1], data[i+2]); if(data[i+3]>10){ data[i] = mappedcolor.r; data[i+1] = mappedcolor.g; data[i+2] = mappedcolor.b; } } ctxmapped.putimagedata(imagedata,0,0); // use euclidian distance find closest color function mapcolortopalette(red,green,blue){ var color,diffr,diffg,diffb,diffdistance,mappedcolor; var distance=25000; for(var i=0;i<palette.length;i++){ color=palette[i]; diffr=( color.r - red ); diffg=( color.g - green ); diffb=( color.b - blue ); diffdistance = diffr*diffr + diffg*diffg + diffb*diffb; if( diffdistance < distance ){ distance=diffdistance; mappedcolor=palette[i]; }; } return(mappedcolor); } }); // end $(function(){}); </script> </head> <body> <canvas id="originalcanvas" width=60 height=100></canvas> <canvas id="mappedcanvas" width=60 height=100></canvas> </body> </html>
Comments
Post a Comment