java - Algorithm to show n different shades of the same color in order of decreasing darkness -
the requirement follows:
we need map values colors. each discrete value have color.
we allow user specify maxcolor
no mincolor
allow them specify number of bins representing number of shades. if maxcolor
selected color.green
, bins= 5
,then have 5 shades of green color selected max being darkest , rest 4 in order of increasing lightness.
//give me list of 5 shades of green first argument being darkest. list<color> greenshades = calculateshades(color.green,5); //give me list of 7 shades of red first argument being darkest. list<color> greenshades = calculateshades(color.red,7);
i tagged question java coding in java. understand algorithm.so implementation/idea of implementation in other languages javascript acceptable.
the basic concept revolves around idea of generating color based on fraction of source...
that is, if want 5 bands, each band 1/5 intensity of last...
public list<color> getcolorbands(color color, int bands) { list<color> colorbands = new arraylist<>(bands); (int index = 0; index < bands; index++) { colorbands.add(darken(color, (double) index / (double) bands)); } return colorbands; } public static color darken(color color, double fraction) { int red = (int) math.round(math.max(0, color.getred() - 255 * fraction)); int green = (int) math.round(math.max(0, color.getgreen() - 255 * fraction)); int blue = (int) math.round(math.max(0, color.getblue() - 255 * fraction)); int alpha = color.getalpha(); return new color(red, green, blue, alpha); }
as quick , nasty example...
import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; import java.util.collections; import java.util.list; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jslider; import javax.swing.timer; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.changeevent; import javax.swing.event.changelistener; public class colorbands { public static void main(string[] args) { new colorbands(); } public colorbands() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private jpanel bandspane; private jslider slider; private timer changetimer; public testpane() { bandspane = new jpanel(new gridbaglayout()); slider = new jslider(1, 100); setlayout(new borderlayout()); add(new jscrollpane(bandspane)); add(slider, borderlayout.south); slider.addchangelistener(new changelistener() { @override public void statechanged(changeevent e) { changetimer.restart(); } }); changetimer = new timer(250, new actionlistener() { @override public void actionperformed(actionevent e) { int bands = slider.getvalue(); list<color> bandslist = getcolorbands(color.red, bands); bandspane.removeall(); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = gridbagconstraints.remainder; gbc.insets = new insets(1, 1, 1, 1); (color color : bandslist) { bandspane.add(new colorband(color), gbc); } gbc.weighty = 1; bandspane.add(new jpanel(), gbc); revalidate(); repaint(); } }); changetimer.setrepeats(false); slider.setvalue(1); } @override public dimension getpreferredsize() { return new dimension(200, 200); } } public list<color> getcolorbands(color color, int bands) { list<color> colorbands = new arraylist<>(bands); (int index = 0; index < bands; index++) { colorbands.add(darken(color, (double) index / (double) bands)); } return colorbands; } public static color darken(color color, double fraction) { int red = (int) math.round(math.max(0, color.getred() - 255 * fraction)); int green = (int) math.round(math.max(0, color.getgreen() - 255 * fraction)); int blue = (int) math.round(math.max(0, color.getblue() - 255 * fraction)); int alpha = color.getalpha(); return new color(red, green, blue, alpha); } public class colorband extends jpanel { public colorband(color color) { setbackground(color); } @override public dimension getpreferredsize() { return new dimension(100, 20); } } }
Comments
Post a Comment