1 24 package info.monitorenter.gui.util; 25 26 import java.util.Iterator ; 27 import java.util.NoSuchElementException ; 28 29 37 public class ColorIterator implements Iterator { 38 39 43 private HSBColor m_start; 44 45 46 private HSBColor m_iterate; 47 48 49 private ColorIterator.ISteppingModel m_stepModel; 50 51 55 private ColorIterator.ISteppingModel m_resetModel; 56 57 62 public ColorIterator() { 63 this.m_start = new HSBColor(0.0f, 1.0f, 1.0f); 65 this.m_iterate = (HSBColor) this.m_start.clone(); 66 this.m_stepModel = new HueStepper(0.001f); 67 this.m_resetModel = (ColorIterator.ISteppingModel) this.m_stepModel.clone(); 68 } 69 70 71 private boolean m_hasnext = true; 72 73 81 public boolean hasNext() { 82 return this.m_hasnext; 83 } 84 85 95 public Object next() throws NoSuchElementException { 96 if (!this.m_hasnext) { 97 throw new java.util.NoSuchElementException ("No more colors to give (call reset for new run!)"); 98 } 99 this.m_stepModel.doStep(this); 100 if (this.m_iterate.equals(this.m_start)) { 101 this.m_hasnext = false; 102 } 103 return this.m_iterate.getRGBColor(); 104 } 105 106 111 public void reset() { 112 this.m_iterate = (HSBColor) this.m_start.clone(); 113 this.m_stepModel = (ColorIterator.ISteppingModel) this.m_resetModel.clone(); 115 this.m_hasnext = true; 116 } 117 118 123 public void remove() { 124 } 126 127 136 public static interface ISteppingModel extends Cloneable { 137 144 public void doStep(final ColorIterator tostep); 145 146 152 public Object clone(); 153 } 154 155 161 public abstract static class DefaultStepping implements ColorIterator.ISteppingModel { 162 163 protected float m_stepping; 164 165 169 public DefaultStepping() { 170 this(1.0f / 100.0f); 171 } 172 173 183 public DefaultStepping(final float stepping) throws IllegalArgumentException { 184 if (stepping > 1.0f || stepping <= 0.0f) { 185 throw new IllegalArgumentException ("Illegal stepping param: choose within 0.0 and 1.0."); 186 } 187 this.m_stepping = stepping; 188 } 189 190 197 public Object clone() { 198 try { 199 DefaultStepping ret = (DefaultStepping) (this.getClass().newInstance()); 200 ret.m_stepping = this.m_stepping; 201 return ret; 202 } catch (Throwable f) { 203 f.printStackTrace(); 205 return null; 206 } 207 } 208 } 209 210 218 public static class HueStepper extends ColorIterator.DefaultStepping { 219 223 public HueStepper() { 224 super(); 225 } 226 227 237 public HueStepper(final float stepping) throws IllegalArgumentException { 238 super(stepping); 239 } 240 241 255 public void doStep(final ColorIterator tostep) { 256 float increment = tostep.m_iterate.m_hue; 257 float bound = tostep.m_start.m_hue; 258 if (increment == bound) { 259 increment += this.m_stepping; 260 } else if (increment < bound) { 261 increment += this.m_stepping; 265 if (increment > bound) { 266 increment = bound; 267 } 268 } else { 269 increment += this.m_stepping; 272 if (increment > 1.0f) { 273 increment -= 1.0; 274 if (increment > bound) { 276 increment = bound; 277 } 278 } 279 } 280 tostep.m_iterate.m_hue = increment; 281 } 282 } 283 284 292 public static class LuminanceStepper extends ColorIterator.DefaultStepping { 293 297 public LuminanceStepper() { 298 } 300 301 312 public LuminanceStepper(final float stepping) throws IllegalArgumentException { 313 super(stepping); 314 } 315 316 330 public void doStep(final ColorIterator tostep) { 331 float increment = tostep.m_iterate.m_lum; 332 float bound = tostep.m_start.m_lum; 333 if (increment == bound) { 334 increment += this.m_stepping; 335 } else if (increment < bound) { 336 increment += this.m_stepping; 340 if (increment > bound) { 341 increment = bound; 342 } 343 } else { 344 increment += this.m_stepping; 347 if (increment > 1.0f) { 348 increment -= 1.0; 349 if (increment > bound) { 350 increment = bound; 351 } 352 } 353 } 354 tostep.m_iterate.m_lum = increment; 355 } 356 } 357 358 366 public static class SaturationStepper extends ColorIterator.DefaultStepping { 367 371 public SaturationStepper() { 372 } 374 375 386 public SaturationStepper(final float stepping) throws IllegalArgumentException { 387 super(stepping); 388 } 389 390 403 public void doStep(final ColorIterator tostep) { 404 float increment = tostep.m_iterate.m_sat; 405 float bound = tostep.m_start.m_sat; 406 if (increment == bound) { 407 increment += this.m_stepping; 408 } else if (increment < bound) { 409 increment += this.m_stepping; 413 if (increment > bound) { 414 increment = bound; 415 } 416 } else { 417 increment += this.m_stepping; 420 if (increment > 1.0f) { 421 increment -= 1.0; 422 if (increment > bound) { 423 increment = bound; 424 } 425 } 426 } 427 tostep.m_iterate.m_sat = increment; 428 } 429 } 430 431 441 public abstract static class PiggyBackStepper extends ColorIterator.DefaultStepping { 442 443 protected HueStepper m_huestep; 444 445 446 protected SaturationStepper m_satstep; 447 448 449 protected LuminanceStepper m_lumstep; 450 451 455 public PiggyBackStepper() { 456 this(0.002f, 0.2f, 0.2f); 457 } 458 459 476 public PiggyBackStepper(final float hueStepping, final float satStepping, 477 final float lumStepping) throws IllegalArgumentException { 478 this.m_huestep = new HueStepper(hueStepping); 479 this.m_satstep = new SaturationStepper(satStepping); 480 this.m_lumstep = new LuminanceStepper(lumStepping); 481 } 482 483 486 public Object clone() { 487 PiggyBackStepper ret = (PiggyBackStepper) super.clone(); 488 ret.m_huestep = (HueStepper) this.m_huestep.clone(); 489 ret.m_satstep = (SaturationStepper) this.m_satstep.clone(); 490 ret.m_lumstep = (LuminanceStepper) this.m_lumstep.clone(); 491 return ret; 492 } 493 } 494 495 506 public static class HSBStepper extends ColorIterator.PiggyBackStepper { 507 511 public HSBStepper() { 512 super(); 513 } 514 515 518 public void doStep(final ColorIterator tostep) { 519 this.m_huestep.doStep(tostep); 523 if (tostep.m_iterate.m_hue == tostep.m_start.m_hue) { 524 this.m_satstep.doStep(tostep); 525 if (tostep.m_iterate.m_sat == tostep.m_start.m_sat) { 526 this.m_lumstep.doStep(tostep); 527 } 528 } 529 } 530 } 531 532 541 public static class HSStepper extends ColorIterator.PiggyBackStepper { 542 546 public HSStepper() { 547 } 549 550 553 public void doStep(final ColorIterator tostep) { 554 this.m_huestep.doStep(tostep); 558 if (tostep.m_iterate.m_hue == tostep.m_start.m_hue) { 559 this.m_satstep.doStep(tostep); 560 } 561 } 562 } 563 564 571 public static void main(final String [] args) { 572 final javax.swing.JFrame frame = new javax.swing.JFrame ("ColorCircleIterator-test"); 573 574 javax.swing.JPanel panel = new javax.swing.JPanel () { 575 578 private static final long serialVersionUID = 3258408422146715703L; 579 580 private ColorIterator m_color = new ColorIterator(); 581 { 582 int wdt = 0; 585 while (this.m_color.hasNext()) { 586 wdt++; 587 this.m_color.next(); 588 } 589 System.out.println("found " + wdt + " different colors."); 590 System.out.println("size: " + wdt); 591 this.setSize(wdt, 100); 592 this.setPreferredSize(new java.awt.Dimension (wdt, 100)); 593 this.setMinimumSize(new java.awt.Dimension (wdt, 100)); 594 } 595 596 599 public void paint(final java.awt.Graphics g) { 600 super.paint(g); 601 this.m_color.reset(); 603 int width = this.getWidth(); 604 int height = this.getHeight(); 605 int pxdrawn = 0; 606 while (this.m_color.hasNext()) { 607 if (pxdrawn == width) { 608 break; 609 } 610 g.setColor((java.awt.Color ) this.m_color.next()); 611 g.drawLine(pxdrawn, 0, pxdrawn, height); 612 pxdrawn++; 613 } 614 } 615 }; 616 617 javax.swing.JScrollPane scroll = new javax.swing.JScrollPane (panel); 618 java.awt.Container contentPane = frame.getContentPane(); 619 contentPane.setLayout(new java.awt.BorderLayout ()); 620 contentPane.add(scroll, java.awt.BorderLayout.CENTER); 621 622 frame.setLocation(200, 200); 623 frame.setSize(new java.awt.Dimension (400, 100)); 624 frame.addWindowListener(new java.awt.event.WindowAdapter () { 625 public void windowClosing(final java.awt.event.WindowEvent e) { 626 System.exit(0); 627 } 628 }); 629 frame.setResizable(true); 630 frame.setVisible(true); 631 } 632 } 633 | Popular Tags |