View | Details | Raw Unified | Return to bug 380894
Collapse All | Expand All

(-)a/org.eclipse.draw2d/src/org/eclipse/draw2d/FigureUtilities.java (-12 / +107 lines)
Lines 26-32 import org.eclipse.draw2d.geometry.Rectangle; Link Here
26
 */
26
 */
27
public class FigureUtilities {
27
public class FigureUtilities {
28
28
29
	private static final float RGB_VALUE_MULTIPLIER = 0.6f;
29
	private static final float VALUE_MULTIPLIER = 0.6f;
30
	private static GC gc;
30
	private static GC gc;
31
	private static Font appliedFont;
31
	private static Font appliedFont;
32
	private static FontMetrics metrics;
32
	private static FontMetrics metrics;
Lines 41-50 public class FigureUtilities { Link Here
41
	 * @since 2.0
41
	 * @since 2.0
42
	 */
42
	 */
43
	public static Color darker(Color color) {
43
	public static Color darker(Color color) {
44
		return new Color(null, (int) (color.getRed() * RGB_VALUE_MULTIPLIER),
44
		double hsl[] = rgbToHSL(original.getRGB());
45
				(int) (color.getGreen() * RGB_VALUE_MULTIPLIER),
45
		hsl[2] = hsl[2] * VALUE_MULTIPLIER;
46
				(int) (color.getBlue() * RGB_VALUE_MULTIPLIER));
46
		return new Color(null, hslToRGB(hsl));
47
	}
47
	}   
48
48
49
	/**
49
	/**
50
	 * Returns the FontMetrics associated with the passed Font.
50
	 * Returns the FontMetrics associated with the passed Font.
Lines 195-206 public class FigureUtilities { Link Here
195
	 * @since 2.0
195
	 * @since 2.0
196
	 */
196
	 */
197
	public static Color lighter(Color rgb) {
197
	public static Color lighter(Color rgb) {
198
		int r = rgb.getRed(), g = rgb.getGreen(), b = rgb.getBlue();
198
		double hsl[] = rgbToHSL(original.getRGB());
199
199
		hsl[2] = Math.min(1.0, hsl[2] / VALUE_MULTIPLIER);
200
		return new Color(null, Math.max(2,
200
		return new Color(null, hslToRGB(hsl));
201
				Math.min((int) (r / RGB_VALUE_MULTIPLIER), 255)), Math.max(2,
202
				Math.min((int) (g / RGB_VALUE_MULTIPLIER), 255)), Math.max(2,
203
				Math.min((int) (b / RGB_VALUE_MULTIPLIER), 255)));
204
	}
201
	}
205
202
206
	/**
203
	/**
Lines 447-451 public class FigureUtilities { Link Here
447
		}
444
		}
448
		return !figBounds.isEmpty();
445
		return !figBounds.isEmpty();
449
	}
446
	}
447
	
448
	
449
	/**
450
	 * Transforms the RGB color into the according HSL color space. Algorithm
451
	 * based on the Book: Computer Graphics: Principles and Practice in C (2nd
452
	 * Edition) (Systems Programming Series) (Hardcover) by James D. Foley
453
	 * (Author), Andries van Dam (Author), Steven K. Feiner (Author), John F.
454
	 * Hughes (Author)
455
	 * 
456
	 * @param rgb the rgb values of the color to convert
457
	 * @return the color in hsl space h: 0..360, s: 0..1, l: 0..1
458
	 */
459
	public static double[] rgbToHSL(RGB rgb) {
460
461
		double r = rgb.red / 255.f;
462
		double g = rgb.green / 255.f;
463
		double b = rgb.blue / 255.f;
464
		double max = Math.max(Math.max(r, g), b);
465
		double min = Math.min(Math.min(r, g), b);
466
467
		double hsl[] = new double[] { 0, 0, (max + min) / 2 };
468
469
		if (max != min) {
470
			// we are not just grey
471
			double delta = max - min;
472
473
			if (hsl[2] <= 0.5) {
474
				hsl[1] = (delta / (max + min));
475
			} else {
476
				hsl[1] = (delta / (2.0 - (max + min)));
477
			}
478
479
			if (r == max) {
480
				hsl[0] = (g - b) / delta;
481
			} else if (g == max) {
482
				hsl[0] = 2.0 + (b - r) / delta;
483
			} else if (b == max) {
484
				hsl[0] = 4.0 + (r - g) / delta;
485
			}
486
			hsl[0] *= 60.0;
487
488
			if (hsl[0] < 0.0) {
489
				hsl[0] += 360.0;
490
			}
491
		}
492
		return hsl;
493
	}
494
495
	/**
496
	 * Transforms the HSL color into the according RGB color space. Algorithm
497
	 * based on the Book: Computer Graphics: Principles and Practice in C (2nd
498
	 * Edition) (Systems Programming Series) (Hardcover) by James D. Foley
499
	 * (Author), Andries van Dam (Author), Steven K. Feiner (Author), John F.
500
	 * Hughes (Author)
501
	 * 
502
	 * @param hsl the color in hsl space h: 0..360, s: 0..1, l: 0..1
503
	 * @return the color in rgb space
504
	 */
505
	public static RGB hslToRGB(double[] hsl) {
506
		RGB retVal = new RGB(0, 0, 0);
507
508
		if (hsl[1] == 0.0) {
509
			if (hsl[0] == 0.0) {
510
				retVal.red = retVal.green = retVal.blue = (int) (hsl[2] * 255.0);
511
			} else {
512
				// in the achromatic (grey) case h must not have a value
513
				SWT.error(SWT.ERROR_INVALID_ARGUMENT);
514
			}
515
		} else {
516
			double m2 = ((hsl[2] <= 0.5) ? hsl[2] * (1.0 + hsl[1]) : hsl[2]
517
					+ hsl[1] - (hsl[2] * hsl[1]));
518
			double m1 = 2.0 * hsl[2] - m2;
519
520
			retVal.red = hslValue(m1, m2, hsl[0] + 120.0);
521
			retVal.green = hslValue(m1, m2, hsl[0]);
522
			retVal.blue = hslValue(m1, m2, hsl[0] - 120.0);
523
		}
524
		return retVal;
525
	}
526
527
	private static int hslValue(double m1, double m2, double hue) {
528
		double retVal = m1;
529
530
		if (hue > 360.0) {
531
			hue -= 360.0;
532
		} else if (hue < 0.0) {
533
			hue += 360.0;
534
		}
535
536
		if (hue < 60.0) {
537
			retVal = m1 + (m2 - m1) * hue / 60.0;
538
		} else if (hue < 180.0) {
539
			retVal = m2;
540
		} else if (hue < 240.0) {
541
			retVal = m1 + (m2 - m1) * (240.0 - hue) / 60.0;
542
		}
543
544
		return (int) (255.0 * retVal);
545
	}
450
546
451
}
547
}
452
- 

Return to bug 380894