User Tools

Site Tools


script_main_color

====== About the script ====== * Purpose : It takes the average color of the icon of an item, and apply it to the selected color. * Author : [[https://plus.google.com/u/1/105066926163073195690|TrianguloY]] * Link: https://plus.google.com/u/1/105066926163073195690/posts/FuBhf8EwF5T ====== How to use the script ====== The script consist of a function (that you can use for other things if you want) that takes an image and a number, and returns the exact average color of the image (using all pixels, half of them,...depends on the number). It can take time, specially with high resolution ones and using all the pixels. If you just want to get the color and don't change it in a while, use all pixels (resolution=1), but if you plan to implement it dynamically, better use less pixels (resolution=66 means approximately 100/66=1.5% of the pixels) You can use it on an item (running it from the item) or in all items in a container (running it from the container). ====== Script code ====== <sxh javascript> var applyto="s";//combination of 'n' 's' 'f' , normal selected focused respectively var resolution=1;//number of pixels that will be used. 1=all, 2=half, 3=third part, and so on var item=LL.getEvent().getItem(); var cont=LL.getEvent().getContainer(); if(!confirm("This will set the color of "+(item!=null?"the item":"all the items in the container")+"\nThis can take a while, are you sure?")) return if(item!=null) colorize(item); else{ var items=cont.getItems(); for(var t=0;t<items.getLength();++t){ if(items.getAt(t).getType()!="Shortcut") continue; colorize(items.getAt(t)); } } function colorize(item){ if(item.getType()!="Shortcut") return; var pic=item.getCustomIcon()||item.getDefaultIcon(); if(pic==null) return; pic = pic.getBitmap(); var prop=item.getProperties().edit() prop.getBox("i.box").setColor("c",applyto,mainColor(pic,resolution)) prop.commit(); } function mainColor(pic,skip){ var siz=[pic.getWidth(),pic.getHeight()]; //color values var r=0; var g=0; var b=0; //number of data (decimal values depending on alpha) var n=0; for(var t=Math.round((siz[0]%skip)/2);t<siz[0];t+=skip) for(var tt=Math.round((siz[1]%skip)/2);tt<siz[1];tt+=skip){ var c=pic.getPixel(t,tt); var a=Color.alpha(c)/255; r+=Color.red(c)*a; g+=Color.green(c)*a; b+=Color.blue(c)*a; n+=a; } return Color.argb(0xff,Math.round(r/n),Math.round(g/n),Math.round(b/n)); } </sxh>

script_main_color.txt · Last modified: 2015/03/17 21:39 by trianguloy