Bitwig Studio Control Surface API  4.0.1
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 
116  public String toHex()
117  {
118  int red = getRed255();
119  int green = getGreen255();
120  int blue = getBlue255();
121  int alpha = getAlpha255();
122 
123 
124  StringBuilder sb = new StringBuilder(9);
125  sb.append("#");
126  addHex2Number(sb, red);
127  addHex2Number(sb, green);
128  addHex2Number(sb, blue);
129 
130  if (alpha != 255)
131  addHex2Number(sb, alpha);
132 
133  return sb.toString();
134  }
135 
136  private void addHex2Number(StringBuilder sb, int x)
137  {
138  String hex = Integer.toHexString(x & 0xff);
139  if (hex.length() == 1) sb.append(0);
140  sb.append(hex);
141  }
142 
143  public static Color nullColor()
144  {
145  return fromRGBA(0, 0, 0, 0);
146  }
147  public static Color blackColor()
148  {
149  return fromRGBA(0, 0, 0, 1);
150  }
151  public static Color whiteColor()
152  {
153  return fromRGBA(1, 1, 1, 1);
154  }
155 
156  public double getRed() { return mRed; }
157  public double getGreen() { return mGreen; }
158  public double getBlue() { return mBlue; }
159  public double getAlpha() { return mAlpha; }
160 
161  public int getRed255() { return (int) (mRed * 255); }
162  public int getGreen255() { return (int) (mGreen * 255); }
163  public int getBlue255() { return (int) (mBlue * 255); }
164  public int getAlpha255() { return (int) (mAlpha * 255); }
165 
166  private final double mRed;
167  private final double mGreen;
168  private final double mBlue;
169  private final double mAlpha;
170 }
static Color mix(final Color c1, final Color c2, double blend)
Definition: Color.java:103