Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

131
LINES

< > BotCompany Repo | #1015058 // RGB - red/green/blue triple with float precision

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (9449L) is out of date.

1  
static class RGB {
2  
  // usually in range [0, 1]
3  
  public float r, g, b; // can't be final cause persistence
4  
  
5  
  *() {}
6  
  
7  
  public RGB(float brightness) {
8  
    r = g = b = brightness;
9  
  }
10  
  
11  
  public RGB(float r, float g, float b) {
12  
    this.r = r;
13  
    this.g = g;
14  
    this.b = b;
15  
  }
16  
17  
  public RGB(double r, double g, double b) {
18  
    this.r = (float) r;
19  
    this.g = (float) g;
20  
    this.b = (float) b;
21  
  }
22  
  
23  
  public RGB(double[] rgb) {
24  
    this(rgb[0], rgb[1], rgb[2]);
25  
  }
26  
27  
  public RGB(int rgb) {
28  
    r = rgbRed(rgb)/255f;
29  
    g = rgbGreen(rgb)/255f;
30  
    b = rgbBlue(rgb)/255f;
31  
  }
32  
  
33  
  public RGB(double brightness) {
34  
    this((float) brightness);
35  
  }
36  
37  
  public RGB(Color color) {
38  
    r = color.getRed()/255f;
39  
    g = color.getGreen()/255f;
40  
    b = color.getBlue()/255f;
41  
  }
42  
43  
  // TODO: 3-char version
44  
  public RGB(S hex) {
45  
    int i = l(hex)-6;
46  
    r = Integer.parseInt(hex.substring(i, i+2), 16)/255f;
47  
    g = Integer.parseInt(hex.substring(i+2, i+4), 16)/255f;
48  
    b = Integer.parseInt(hex.substring(i+4, i+6), 16)/255f;
49  
  }
50  
51  
  public float getComponent(int i) {
52  
    return i == 0 ? r : i == 1 ? g : b;
53  
  }
54  
55  
  public int getInt(int i) {
56  
    ret i == 0 ? redInt() : i == 1 ? greenInt() : blueInt();
57  
  }
58  
  
59  
  public Color getColor() {
60  
    return new Color(clampZeroToOne(r), clampZeroToOne(g), clampZeroToOne(b));
61  
  }
62  
63  
  public static RGB newSafe(float r, float g, float b) {
64  
    return new RGB(Math.max(0, Math.min(1, r)), Math.max(0, Math.min(1, g)), Math.max(0, Math.min(1, b)));
65  
  }
66  
67  
  int asInt() { return getColor().getRGB() & 0xFFFFFF; }
68  
  int getInt() { return getColor().getRGB() & 0xFFFFFF; }
69  
  int asIntWithAlpha() { return rgbInt(redInt(), greenInt(), blueInt()) | 0xFF000000; }
70  
71  
  public float getBrightness aka brightness() {
72  
    return (r+g+b)/3.0f;
73  
  }
74  
75  
  public String getHexString() {
76  
    return Integer.toHexString(asInt() | 0xFF000000).substring(2).toUpperCase();
77  
  }
78  
79  
  @Override
80  
  public boolean equals(Object o) {
81  
    if (this == o) return true;
82  
    if (!(o instanceof RGB)) return false;
83  
84  
    RGB rgb = (RGB) o;
85  
86  
    if (Float.compare(rgb.b, b) != 0) return false;
87  
    if (Float.compare(rgb.g, g) != 0) return false;
88  
    if (Float.compare(rgb.r, r) != 0) return false;
89  
90  
    return true;
91  
  }
92  
93  
  @Override
94  
  public int hashCode() {
95  
    int result = (r != +0.0f ? Float.floatToIntBits(r) : 0);
96  
    result = 31 * result + (g != +0.0f ? Float.floatToIntBits(g) : 0);
97  
    result = 31 * result + (b != +0.0f ? Float.floatToIntBits(b) : 0);
98  
    return result;
99  
  }
100  
101  
  public boolean isBlack() {
102  
    return r == 0f && g == 0f && b == 0f;
103  
  }
104  
105  
  public boolean isWhite() {
106  
    return r == 1f && g == 1f && b == 1f;
107  
  }
108  
  
109  
  // This is hyperprecise
110  
  bool isGrayscale() {
111  
    ret r == g && g == b;
112  
  }
113  
114  
  public String toString() {
115  
    //return getHexString();
116  
    ret isGrayscale() ? formatChannel(r)
117  
      : spaceCombine("RGB", formatChannel(r), formatChannel(g), formatChannel(b));
118  
  }
119  
  
120  
  S formatChannel(float value) {
121  
    ret formatDouble(value, 2);
122  
  }
123  
  
124  
  int redInt() { ret iround(r*255); }
125  
  int greenInt() { ret iround(g*255); }
126  
  int blueInt() { ret iround(b*255); }
127  
  
128  
  static float brightnessToFloat(int brightness) { ret brightness/255f; }
129  
  
130  
  RGB cloneMe() { ret new RGB(r, g, b); }
131  
}

Author comment

Began life as a copy of #1002470

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, podlckwnjdmb, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1015058
Snippet name: RGB - red/green/blue triple with float precision
Eternal ID of this version: #1015058/23
Text MD5: 8f5d6e8591810f73086dd65508c323f9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-21 12:41:05
Source code size: 3353 bytes / 131 lines
Pitched / IR pitched: No / No
Views / Downloads: 537 / 1954
Version history: 22 change(s)
Referenced in: [show references]