1 package org.robolectric.shadows;
2 
3 import android.graphics.Color;
4 import org.robolectric.annotation.Implementation;
5 import org.robolectric.annotation.Implements;
6 
7 @Implements(Color.class)
8 public class ShadowColor {
9   /**
10    * This is implemented in native code in the Android SDK.
11    *
12    * <p>Since HSV == HSB then the implementation from {@link java.awt.Color} can be used, with a
13    * small adjustment to the representation of the hue.
14    *
15    * <p>{@link java.awt.Color} represents hue as 0..1 (where 1 == 100% == 360 degrees), while {@link
16    * android.graphics.Color} represents hue as 0..360 degrees. The correct hue can be calculated by
17    * multiplying with 360.
18    *
19    * @param red Red component
20    * @param green Green component
21    * @param blue Blue component
22    * @param hsv Array to store HSV components
23    */
24   @Implementation
RGBToHSV(int red, int green, int blue, float hsv[])25   protected static void RGBToHSV(int red, int green, int blue, float hsv[]) {
26     java.awt.Color.RGBtoHSB(red, green, blue, hsv);
27     hsv[0] = hsv[0] * 360;
28   }
29 
30   @Implementation
HSVToColor(int alpha, float hsv[])31   protected static int HSVToColor(int alpha, float hsv[]) {
32     int rgb = java.awt.Color.HSBtoRGB(hsv[0] / 360, hsv[1], hsv[2]);
33     return Color.argb(alpha, Color.red(rgb), Color.green(rgb), Color.blue(rgb));
34   }
35 }
36