Control Surface API  3.0.3
Color.java
1 package com.bitwig.extension.api;
2 
8 public class Color
9 {
10  private Color(final double red, final double green, final double blue, final double alpha)
11  {
12  mRed = red;
13  mGreen = green;
14  mBlue = blue;
15  mAlpha = alpha;
16  }
17 
18  public static Color fromRGB(double red, double green, double blue)
19  {
20  return new Color(red, green, blue, 1);
21  }
22 
23  public static Color fromRGBA(double red, double green, double blue, double alpha)
24  {
25  return new Color(red, green, blue, alpha);
26  }
27 
28  public static Color fromRGB255(final int red, final int green, final int blue)
29  {
30  return new Color(red / 255.0, green / 255.0, blue / 255.0, 1);
31  }
32 
33  public static Color fromRGBA255(final int red, final int green, final int blue, final int alpha)
34  {
35  return new Color(red / 255.0, green / 255.0, blue / 255.0, alpha / 255.0);
36  }
37 
38  public static Color fromHex(String hex)
39  {
40  if (hex.isEmpty())
41  return nullColor();
42 
43  // Skip the initialial '#'
44  if (hex.charAt(0) == '#')
45  hex = hex.substring(1);
46 
47  // Check that the color is an hex value
48  for (int i = 0; i < hex.length(); ++i)
49  {
50  char c = hex.charAt(i);
51  if (('0' <= c && c <= '9') ||
52  ('a' <= c && c <= 'f') ||
53  ('A' <= c && c <= 'F'))
54  continue;
55 
56  return nullColor();
57  }
58 
59  switch (hex.length())
60  {
61  case 3: // RGB
62  {
63  int r = Integer.parseInt(hex.substring(0, 1), 16);
64  int g = Integer.parseInt(hex.substring(1, 2), 16);
65  int b = Integer.parseInt(hex.substring(2, 3), 16);
66  return fromRGB(r / 15.0, g / 15.0, b / 15.0);
67  }
68 
69  case 4: // RGBA
70  {
71  int r = Integer.parseInt(hex.substring(0, 1), 16);
72  int g = Integer.parseInt(hex.substring(1, 2), 16);
73  int b = Integer.parseInt(hex.substring(2, 3), 16);
74  int a = Integer.parseInt(hex.substring(2, 3), 16);
75  return fromRGBA(r / 15.0, g / 15.0, b / 15.0, a / 15.0);
76  }
77 
78  case 6: // RRGGBB
79  {
80  int r = Integer.parseInt(hex.substring(0, 2), 16);
81  int g = Integer.parseInt(hex.substring(2, 4), 16);
82  int b = Integer.parseInt(hex.substring(4, 6), 16);
83  return fromRGB255(r, g, b);
84  }
85 
86  case 8: // RRGGBBAA
87  {
88  int r = Integer.parseInt(hex.substring(0, 2), 16);
89  int g = Integer.parseInt(hex.substring(2, 4), 16);
90  int b = Integer.parseInt(hex.substring(4, 6), 16);
91  int a = Integer.parseInt(hex.substring(6, 8), 16);
92  return fromRGBA255(r, g, b, a);
93  }
94  }
95 
96  return nullColor();
97  }
98 
103  public static Color mix(final Color c1, final Color c2, double blend)
104  {
105  final double b1 = blend;
106  final double b2 = 1 - blend;
107 
108  return new Color(
109  c1.getRed() * b1 + c2.getRed() * b2,
110  c1.getGreen() * b1 + c2.getGreen() * b2,
111  c1.getBlue() * b1 + c2.getBlue() * b2,
112  c1.getAlpha() * b1 + c2.getAlpha() * b2);
113  }
114 
115  public static Color nullColor()
116  {
117  return fromRGBA(0, 0, 0, 0);
118  }
119  public static Color blackColor()
120  {
121  return fromRGBA(0, 0, 0, 1);
122  }
123  public static Color whiteColor()
124  {
125  return fromRGBA(1, 1, 1, 1);
126  }
127 
128  public double getRed() { return mRed; }
129  public double getGreen() { return mGreen; }
130  public double getBlue() { return mBlue; }
131  public double getAlpha() { return mAlpha; }
132 
133  public int getRed255() { return (int) (mRed * 255); }
134  public int getGreen255() { return (int) (mGreen * 255); }
135  public int getBlue255() { return (int) (mBlue * 255); }
136  public int getAlpha255() { return (int) (mAlpha * 255); }
137 
138  private final double mRed;
139  private final double mGreen;
140  private final double mBlue;
141  private final double mAlpha;
142 }
static Color mix(final Color c1, final Color c2, double blend)
Definition: Color.java:103