[Arakhnę-Dev] [399] * Add utility functions in VectorToolkit.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 399
Author:   galland
Date:     2013-03-22 22:56:37 +0100 (Fri, 22 Mar 2013)
Log Message:
-----------
* Add utility functions in VectorToolkit.

Modified Paths:
--------------
    trunk/ui/ui-vector/src/main/java/org/arakhne/afc/ui/vector/Color.java
    trunk/ui/ui-vector/src/main/java/org/arakhne/afc/ui/vector/VectorToolkit.java
    trunk/ui/ui-vector-android/src/main/java/org/arakhne/afc/ui/vector/android/AndroidColor.java
    trunk/ui/ui-vector-android/src/main/java/org/arakhne/afc/ui/vector/android/AndroidVectorToolkit.java
    trunk/ui/ui-vector-awt/src/main/java/org/arakhne/afc/ui/vector/awt/AwtColor.java
    trunk/ui/ui-vector-awt/src/main/java/org/arakhne/afc/ui/vector/awt/AwtVectorToolkit.java

Modified: trunk/ui/ui-vector/src/main/java/org/arakhne/afc/ui/vector/Color.java
===================================================================
--- trunk/ui/ui-vector/src/main/java/org/arakhne/afc/ui/vector/Color.java	2013-03-22 13:52:38 UTC (rev 398)
+++ trunk/ui/ui-vector/src/main/java/org/arakhne/afc/ui/vector/Color.java	2013-03-22 21:56:37 UTC (rev 399)
@@ -124,4 +124,48 @@
 	 */
 	public Color transparentColor();
 
+    /**
+     * Returns the HSB model representing the color.
+     * 
+     * The <code>saturation</code> and <code>brightness</code>
+     * components should be
+     * floating-point values between zero and one
+     * (numbers in the range 0.0-1.0).  The <code>hue</code> component
+     * can be any floating-point number.  The floor of this number is
+     * subtracted from it to create a fraction between 0 and 1.  This
+     * fractional number is then multiplied by 360 to produce the hue
+     * angle in the HSB color model.
+     * 
+     * @return the HSB value of the color.
+     */
+    public float[] getHSB();
+
+    /**
+     * Returns Saturation in the the HSB model representing the color.
+     * 
+     * @return the <code>saturation</code>, a
+     * floating-point values between zero and one
+     * (numbers in the range 0.0-1.0).
+     */
+    public float getSaturation();
+
+    /**
+     * Returns Brightness in the the HSB model representing the color.
+     * 
+     * @return the <code>brightness</code>, a
+     * floating-point values between zero and one
+     * (numbers in the range 0.0-1.0).
+     */
+    public float getBrightness();
+
+    /**
+     * Returns the Hue in the HSB model representing the color.
+     * 
+     * @return the <code>hue</code>, can be any floating-point number.
+     * The floor of this number is subtracted from it to create a
+     * fraction between 0 and 1.  This fractional number is then
+     * multiplied by 360 to produce the hue angle in the HSB color model.
+     */
+    public float getHue();
+
 }

Modified: trunk/ui/ui-vector/src/main/java/org/arakhne/afc/ui/vector/VectorToolkit.java
===================================================================
--- trunk/ui/ui-vector/src/main/java/org/arakhne/afc/ui/vector/VectorToolkit.java	2013-03-22 13:52:38 UTC (rev 398)
+++ trunk/ui/ui-vector/src/main/java/org/arakhne/afc/ui/vector/VectorToolkit.java	2013-03-22 21:56:37 UTC (rev 399)
@@ -597,7 +597,7 @@
 	 * The value is in <code>[-1;1]</code>.
 	 * A value of <code>-1</code> means that the alpha-component is set to none.
 	 * A value of <code>1</code> means that the alpha-component is completely
-	 * set. A value of <code>0</code> means that the alpha-component
+	 * set. A value of <code>0</code> means that the alpha-component not changed.
 	 * 
 	 * @param imageObject
 	 * @param transparency is the transparency to apply to the given image
@@ -611,7 +611,7 @@
 	 * The value is in <code>[-1;1]</code>.
 	 * A value of <code>-1</code> means that the alpha-component is set to none.
 	 * A value of <code>1</code> means that the alpha-component is completely
-	 * set. A value of <code>0</code> means that the alpha-component
+	 * set. A value of <code>0</code> means that the alpha-component is not changed.
 	 * 
 	 * @param imageObject
 	 * @param transparency is the transparency to apply to the given image
@@ -621,6 +621,36 @@
 	 */
 	protected abstract Image makeTransparentImage(Image imageObject, float transparency);
 
+    /** Replies the specified image filtered with the given color.
+	 * 
+	 * @param imageObject is the image to filter.
+     * @param filtering_color is the color used to filter.
+     * @param alpha indicates how the icon and the color are merged. The value
+     * is in <code>[0;1]</code>. A value of <code>0</code> means that the color 
+     * of the icon is used. A value of <code>1</code> means that the given color
+     * is used in place of the original colors. A value of <code>0.5</code>
+     * means that the resulting color is at half way between the icon's color
+     * and the given color. 
+	 * @return the filtered image or <code>null</code>.
+	 */
+	public static Image image(Image imageObject, Color filtering_color, float alpha) {
+		return SINGLETON.createColorizedImage(imageObject, filtering_color, alpha);
+	}
+
+    /** Replies the specified image filtered with the given color.
+	 * 
+	 * @param imageObject is the image to filter.
+     * @param filtering_color is the color used to filter.
+     * @param alpha indicates how the icon and the color are merged. The value
+     * is in <code>[0;1]</code>. A value of <code>0</code> means that the color 
+     * of the icon is used. A value of <code>1</code> means that the given color
+     * is used in place of the original colors. A value of <code>0.5</code>
+     * means that the resulting color is at half way between the icon's color
+     * and the given color. 
+	 * @return the filtered image or <code>null</code>.
+	 */
+	protected abstract Image createColorizedImage(Image imageObject, Color filtering_color, float alpha);
+
 	/** Write the image.
 	 * 
 	 * @param image is the image to write.
@@ -641,6 +671,34 @@
 	 */
 	protected abstract void write(Image image, String type, OutputStream stream) throws IOException;
 
+	/** Replies the system background selection color.
+	 * 
+	 * @return the color of the background for the color.
+	 */
+	public static Color getSelectionBackground() {
+		return SINGLETON.createSelectionBackground();
+	}
+	
+	/** Replies the system background selection color.
+	 * 
+	 * @return the color of the background for the color.
+	 */
+	protected abstract Color createSelectionBackground();
+
+	/** Replies the system background selection color.
+	 * 
+	 * @return the color of the background for the color.
+	 */
+	public static Color getSelectionForeground() {
+		return SINGLETON.createSelectionBackground();
+	}
+	
+	/** Replies the system background selection color.
+	 * 
+	 * @return the color of the background for the color.
+	 */
+	protected abstract Color createSelectionForeground();
+
 	//------------------------------
 	// STROKE
 	//------------------------------
@@ -893,7 +951,7 @@
 		return SINGLETON.createColor(rawColorObject);
 	}
 
-    /** Create a color from a underground API color.
+	/** Create a color from a underground API color.
 	 * <p>
 	 * The type of the native affine transform depends on the underlying graphic
 	 * API. It is a <code>java.awt.Color</code> for AWT.
@@ -903,6 +961,81 @@
 	 */
 	protected abstract Color createColor(Object rawColorObject);
 
+    /** Decode the color from a string.
+     * The supported formats are :
+     * #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white',
+     * 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray',
+     * 'orange', 'pink'.
+     * This method handles string
+     * formats that are used to represent octal and hexadecimal numbers.
+	 * 
+	 * @param c is the string representation of the color. 
+	 * @return the generic color.
+	 */
+	public static Color color(String c) {
+		if ("red".equalsIgnoreCase(c)) return RED; //$NON-NLS-1$
+		if ("blue".equalsIgnoreCase(c)) return BLUE; //$NON-NLS-1$
+		if ("green".equalsIgnoreCase(c)) return GREEN; //$NON-NLS-1$
+		if ("black".equalsIgnoreCase(c)) return BLACK; //$NON-NLS-1$
+		if ("white".equalsIgnoreCase(c)) return WHITE; //$NON-NLS-1$
+		if ("gray".equalsIgnoreCase(c)) return GRAY; //$NON-NLS-1$
+		if ("cyan".equalsIgnoreCase(c)) return CYAN; //$NON-NLS-1$
+		if ("magenta".equalsIgnoreCase(c)) return MAGENTA; //$NON-NLS-1$
+		if ("yellow".equalsIgnoreCase(c)) return YELLOW; //$NON-NLS-1$
+		if ("lightgray".equalsIgnoreCase(c)) return LIGHT_GRAY; //$NON-NLS-1$
+		if ("darkgray".equalsIgnoreCase(c)) return DARK_GRAY; //$NON-NLS-1$
+		if ("orange".equalsIgnoreCase(c)) return ORANGE; //$NON-NLS-1$
+		if ("pink".equalsIgnoreCase(c)) return PINK; //$NON-NLS-1$
+		Integer intval = Integer.decode(c);
+        int i = intval.intValue();
+        int a;
+        if (i>0xFFFFFF) a = (i >> 32) & 0xFF;
+        else a = 255;
+        int r = (i >> 16) & 0xFF;
+        int g = (i >> 8) & 0xFF;
+        int b = i & 0xFF;
+		return color(r, g, b, a);
+	}
+
+    /** Creates a <code>Color</code> object based on the specified values
+     * for the HSB color model.
+     * <p>
+     * The <code>s</code> and <code>b</code> components should be
+     * floating-point values between zero and one
+     * (numbers in the range 0.0-1.0).  The <code>h</code> component
+     * can be any floating-point number.  The floor of this number is
+     * subtracted from it to create a fraction between 0 and 1.  This
+     * fractional number is then multiplied by 360 to produce the hue
+     * angle in the HSB color model.
+     * @param  h   the hue component
+     * @param  s   the saturation of the color
+     * @param  b   the brightness of the color
+	 * @return the generic color.
+	 */
+	public static Color colorHSB(float h, float s, float b) {
+		return color(SINGLETON.HSBtoRGB(h, s, b));
+	}
+	
+    /**
+     * Converts the components of a color, as specified by the HSB
+     * model, to an equivalent set of values for the default RGB model.
+     * <p>
+     * The <code>saturation</code> and <code>brightness</code> components
+     * should be floating-point values between zero and one
+     * (numbers in the range 0.0-1.0).  The <code>hue</code> component
+     * can be any floating-point number.  The floor of this number is
+     * subtracted from it to create a fraction between 0 and 1.  This
+     * fractional number is then multiplied by 360 to produce the hue
+     * angle in the HSB color model.
+
+     * @param     hue   the hue component of the color
+     * @param     saturation   the saturation of the color
+     * @param     brightness   the brightness of the color
+     * @return    the RGB value of the color with the indicated hue,
+     *                            saturation, and brightness.
+     */
+    protected abstract int HSBtoRGB(float hue, float saturation, float brightness);
+	
 	//------------------------------
 	// STATIC INIT
 	//------------------------------

Modified: trunk/ui/ui-vector-android/src/main/java/org/arakhne/afc/ui/vector/android/AndroidColor.java
===================================================================
--- trunk/ui/ui-vector-android/src/main/java/org/arakhne/afc/ui/vector/android/AndroidColor.java	2013-03-22 13:52:38 UTC (rev 398)
+++ trunk/ui/ui-vector-android/src/main/java/org/arakhne/afc/ui/vector/android/AndroidColor.java	2013-03-22 21:56:37 UTC (rev 399)
@@ -188,4 +188,32 @@
 		return Integer.toHexString(this.original);
 	}
 
+	@Override
+	public float[] getHSB() {
+		float[] t = new float[3];
+		android.graphics.Color.RGBToHSV(getRed(), getGreen(), getBlue(), t);
+		return t;
+	}
+
+	@Override
+	public float getSaturation() {
+		float[] t = new float[3];
+		android.graphics.Color.RGBToHSV(getRed(), getGreen(), getBlue(), t);
+		return t[1];
+	}
+
+	@Override
+	public float getBrightness() {
+		float[] t = new float[3];
+		android.graphics.Color.RGBToHSV(getRed(), getGreen(), getBlue(), t);
+		return t[2];
+	}
+
+	@Override
+	public float getHue() {
+		float[] t = new float[3];
+		android.graphics.Color.RGBToHSV(getRed(), getGreen(), getBlue(), t);
+		return t[0];
+	}
+
 }
\ No newline at end of file

Modified: trunk/ui/ui-vector-android/src/main/java/org/arakhne/afc/ui/vector/android/AndroidVectorToolkit.java
===================================================================
--- trunk/ui/ui-vector-android/src/main/java/org/arakhne/afc/ui/vector/android/AndroidVectorToolkit.java	2013-03-22 13:52:38 UTC (rev 398)
+++ trunk/ui/ui-vector-android/src/main/java/org/arakhne/afc/ui/vector/android/AndroidVectorToolkit.java	2013-03-22 21:56:37 UTC (rev 399)
@@ -280,4 +280,55 @@
 		return new AndroidPdf(url);
 	}
 
+	@Override
+	protected int HSBtoRGB(float hue, float saturation, float brightness) {
+		float[] t = new float[] {hue, saturation, brightness};
+		return android.graphics.Color.HSVToColor(t);
+	}
+
+	@Override
+	protected Image createColorizedImage(Image imageObject,
+			Color filtering_color, float alpha) {
+		if (filtering_color==null || imageObject==null) return imageObject;
+		Bitmap img = toNativeUIObject(Bitmap.class, imageObject);
+		Bitmap mutableBitmap = img.copy(Bitmap.Config.ARGB_8888, true);
+		int[] pixels = new int[mutableBitmap.getHeight()*mutableBitmap.getWidth()];
+		mutableBitmap.getPixels(pixels, 0, mutableBitmap.getWidth(), 0, 0, mutableBitmap.getWidth(), mutableBitmap.getHeight());
+        
+        int color_r, color_g, color_b;
+        
+        for(int i=0; i<pixels.length; ++i) {
+			color_r = ((pixels[i] >> 16) & 0xff); 
+			color_g = ((pixels[i] >> 8) & 0xff); 
+			color_b = (pixels[i] & 0xff);
+
+			color_r = (int)((filtering_color.getRed() + color_r) * alpha);
+			if (color_r<0) color_r = 0;
+			if (color_r>255) color_r = 0;
+
+			color_g = (int)((filtering_color.getGreen() + color_g) * alpha);
+			if (color_g<0) color_g = 0;
+			if (color_g>255) color_g = 0;
+
+			color_b = (int)((filtering_color.getBlue() + color_b) * alpha);
+			if (color_b<0) color_b = 0;
+			if (color_b>255) color_b = 0;
+
+			pixels[i] = (pixels[i] & 0xff000000) | (color_r << 16) | (color_g << 8) | color_b;
+        }
+        
+        mutableBitmap.setPixels(pixels, 0, mutableBitmap.getWidth(), 0, 0, mutableBitmap.getWidth(), mutableBitmap.getHeight());
+		return new AndroidImage(mutableBitmap);
+	}
+
+	@Override
+	protected Color createSelectionBackground() {
+		return color(0xFF9ccf00);
+	}
+
+	@Override
+	protected Color createSelectionForeground() {
+		return color(0xFF000000);
+	}
+
 }
\ No newline at end of file

Modified: trunk/ui/ui-vector-awt/src/main/java/org/arakhne/afc/ui/vector/awt/AwtColor.java
===================================================================
--- trunk/ui/ui-vector-awt/src/main/java/org/arakhne/afc/ui/vector/awt/AwtColor.java	2013-03-22 13:52:38 UTC (rev 398)
+++ trunk/ui/ui-vector-awt/src/main/java/org/arakhne/afc/ui/vector/awt/AwtColor.java	2013-03-22 21:56:37 UTC (rev 399)
@@ -71,4 +71,27 @@
 		return type.cast(this);
 	}
 
+	@Override
+	public float[] getHSB() {
+		return RGBtoHSB(getRed(), getGreen(), getBlue(), null);
+	}
+
+	@Override
+	public float getSaturation() {
+		float[] t = RGBtoHSB(getRed(), getGreen(), getBlue(), null);
+		return t[1];
+	}
+
+	@Override
+	public float getBrightness() {
+		float[] t = RGBtoHSB(getRed(), getGreen(), getBlue(), null);
+		return t[2];
+	}
+
+	@Override
+	public float getHue() {
+		float[] t = RGBtoHSB(getRed(), getGreen(), getBlue(), null);
+		return t[0];
+	}
+
 }
\ No newline at end of file

Modified: trunk/ui/ui-vector-awt/src/main/java/org/arakhne/afc/ui/vector/awt/AwtVectorToolkit.java
===================================================================
--- trunk/ui/ui-vector-awt/src/main/java/org/arakhne/afc/ui/vector/awt/AwtVectorToolkit.java	2013-03-22 13:52:38 UTC (rev 398)
+++ trunk/ui/ui-vector-awt/src/main/java/org/arakhne/afc/ui/vector/awt/AwtVectorToolkit.java	2013-03-22 21:56:37 UTC (rev 399)
@@ -23,6 +23,7 @@
 
 import java.awt.AlphaComposite;
 import java.awt.Polygon;
+import java.awt.Toolkit;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Arc2D;
 import java.awt.geom.Area;
@@ -41,6 +42,8 @@
 import java.net.URL;
 
 import javax.imageio.ImageIO;
+import javax.swing.GrayFilter;
+import javax.swing.UIManager;
 
 import org.arakhne.afc.math.continous.object2d.Circle2f;
 import org.arakhne.afc.math.continous.object2d.Ellipse2f;
@@ -427,4 +430,129 @@
 		return new AwtPdf(url);
 	}
 
+	@Override
+	protected int HSBtoRGB(float hue, float saturation, float brightness) {
+		return java.awt.Color.HSBtoRGB(hue, saturation, brightness);
+	}
+
+	@Override
+	protected Image createColorizedImage(Image imageObject,
+			Color filtering_color, float alpha) {
+		if (filtering_color==null || imageObject==null) return imageObject;
+		java.awt.Image source = nativeUIObject(java.awt.Image.class, imageObject);
+        if (source==null) return null;
+        java.awt.Image img = ColorFilter.createFilteredImage(
+        		source,
+        		nativeUIObject(java.awt.Color.class, filtering_color),
+        		alpha);
+        return new AwtImage(img);
+	}
+
+	/** This class permits to filter the color of an icon.
+	 * 
+	 * @author $Author: sgalland$
+	 * @version $FullVersion$
+	 * @mavengroupid $GroupId$
+	 * @mavenartifactid $ArtifactId$
+	 */
+	private static class ColorFilter extends java.awt.image.RGBImageFilter {
+
+		/** Indicates the filtering color.
+		 */
+		private final int red;
+
+		/** Indicates the filtering color.
+		 */
+		private final int blue;
+
+		/** Indicates the filtering color.
+		 */
+		private final int green;
+
+		/** Alpha blending value.
+		 */
+		private final float alpha;
+
+		/**
+		 * Creates a color-filtered image
+		 * 
+		 * @param i is the image to filter
+		 * @param filtering_color is the color used to filter.
+		 * @param alpha indicates how the icon and the color are merged. The value
+		 * is in <code>[0;1]</code>. A value of <code>0</code> means that the color 
+		 * of the icon is used. A value of <code>1</code> means that the given color
+		 * is used in place of the original colors. A value of <code>0.5</code>
+		 * means that the resulting color is at half way between the icon's color
+		 * and the given color. 
+		 * @return the result of the filtering.
+		 */
+		public static java.awt.Image createFilteredImage(java.awt.Image i, java.awt.Color filtering_color, float alpha) {
+			ColorFilter filter = new ColorFilter(filtering_color, alpha);
+			java.awt.image.ImageProducer prod = new java.awt.image.FilteredImageSource(i.getSource(), filter);
+			java.awt.Image filteredImage = Toolkit.getDefaultToolkit().createImage(prod);
+			return filteredImage;
+		}
+
+		/**
+		 * Constructs a ColorFilter object that filters a color image to a 
+		 * color-scale image. It is similar to {@link GrayFilter} but not for
+		 * a specifical color. 
+		 *
+		 * @param filtering_color is the color used to filter.
+		 * @param alpha indicates how the icon and the color are merged. The value
+		 * is in <code>[0;1]</code>. A value of <code>0</code> means that the color 
+		 * of the icon is used. A value of <code>1</code> means that the given color
+		 * is used in place of the original colors. A value of <code>0.5</code>
+		 * means that the resulting color is at half way between the icon's color
+		 * and the given color. 
+		 */
+		public ColorFilter(java.awt.Color filtering_color, float alpha) {
+			this.red = filtering_color.getRed();
+			this.green = filtering_color.getGreen();
+			this.blue = filtering_color.getBlue();
+			this.alpha = (alpha<0f) ? 0f : ((alpha>1f) ? 1f : alpha);
+
+			// canFilterIndexColorModel indicates whether or not it is acceptable
+			// to apply the color filtering of the filterRGB method to the color
+			// table entries of an IndexColorModel object in lieu of pixel by pixel
+			// filtering.
+			this.canFilterIndexColorModel = true;
+		}
+
+		/**
+		 * Filter the specified color.
+		 */
+		@Override
+		public int filterRGB(int x, int y, int rgb) {
+			int color_r = ((rgb >> 16) & 0xff); 
+			int color_g = ((rgb >> 8) & 0xff); 
+			int color_b = (rgb & 0xff);
+
+			color_r = (int)((this.red + color_r) * this.alpha);
+			if (color_r<0) color_r = 0;
+			if (color_r>255) color_r = 0;
+
+			color_g = (int)((this.green + color_g) * this.alpha);
+			if (color_g<0) color_g = 0;
+			if (color_g>255) color_g = 0;
+
+			color_b = (int)((this.blue + color_b) * this.alpha);
+			if (color_b<0) color_b = 0;
+			if (color_b>255) color_b = 0;
+
+			return (rgb & 0xff000000) | (color_r << 16) | (color_g << 8) | color_b;
+		}
+
+	}
+
+	@Override
+	protected Color createSelectionBackground() {
+		return color(UIManager.getColor("Tree.selectionBackground")); //$NON-NLS-1$
+	}
+
+	@Override
+	protected Color createSelectionForeground() {
+		return color(UIManager.getColor("Tree.selectionForeground")); //$NON-NLS-1$
+	}
+
 }
\ No newline at end of file


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/