2

I want to use Color ,in my GWT client side ,

i want this kind of color

                 public static Color myColor = new Color( 152, 207, 204) ;

if i use this import

                    import java.awt.Color;

at Client side it gives me error:

             No source code is available for type java.awt.Color; did you forget to inherit a required module

How can i use RGB colors in GWT client side , by NOT using CSS.

junaidp
  • 10,801
  • 29
  • 89
  • 137

4 Answers4

5

You can write a simple RGB-to-String converter:

public final  class Helper {
    public static String RgbToHex(int r, int g, int b){
      StringBuilder sb = new StringBuilder();
      sb.append('#')
      .append(Integer.toHexString(r))
      .append(Integer.toHexString(g))
      .append(Integer.toHexString(b));
      return sb.toString();
    }
}

And use it:

nameField.getElement().getStyle().setBackgroundColor(Helper.RgbToHex(50, 100, 150));

---Update---

More complex way with controlling of negative value, great than 255, and 0-15 value.

  public static String RgbToHex(int r, int g, int b){
    StringBuilder sb = new StringBuilder();
    sb.append('#')
    .append(intTo2BytesStr(r))
    .append(intTo2BytesStr(g))
    .append(intTo2BytesStr(b));
    return sb.toString();
  }

  private static String intTo2BytesStr(int i) {
    return pad(Integer.toHexString(intTo2Bytes(i)));
  }

  private static int intTo2Bytes(int i){
    return (i < 0) ? 0 : (i > 255) ? 255 : i;
  }

  private static String pad(String str){
    StringBuilder sb = new StringBuilder(str);
    if (sb.length()<2){
      sb.insert(0, '0');
    }
    return sb.toString();
  }
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
  • I don't think this is going to work well for R/G/B values below 16 decimal (10 hex) ... for example, (5, 10, 20) will result in a string that contains "#5a14" ... not what you want! – Andy King Mar 18 '13 at 04:45
  • @Andy King You right. But here is some other problems: 1. Negative value of int. 2. Value > than 255. – A1exandr Belan Mar 18 '13 at 08:27
  • @FFire - Sure, you can add checks for invalid values; I was just pointing out that **valid** values were not all handled correctly. (btw, I'd recommend that, if you're going to use a `StringBuilder`, you should pass the one object around among your helper routines, instead of creating four `StringBuilder` instances every time the `RgbToHex` method is invoked). – Andy King Mar 19 '13 at 01:14
2

You are using AWT class of Color.

GWT != Java .  //so gwt compiler wont compile the awt classes 

Use this ThirdParty Color Class instead.

Just copy that class into your utility package and use it on client side.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • thanks , actually i am using it in CellTable with extended rows , So i was previously using this for making my columns color: td.style().trustedBackgroundColor("blue"); :: Now its not taking anything except String , Any way i can give these RGB colors to my celltable Cell. – junaidp Mar 14 '13 at 07:44
  • May be the method takes only w3c slandered colors ..here is the list of standered colrs http://www.docsteve.com/DocSteve/TechNotes/color_test.html. – Suresh Atta Mar 14 '13 at 07:51
  • thanks , +1 for this list , it will be helpful , but ideally to have some way to put the RGB colors of my own – junaidp Mar 14 '13 at 07:54
  • have you tried with the getHexValue() method in the class which i gave ??it will return the color code like "#000000" then you can pass i think. – Suresh Atta Mar 14 '13 at 07:57
0

You used awt api color here. but GWT does not emulate this library. Refer:

bNd
  • 7,512
  • 7
  • 39
  • 72
0

Here's a more correct version of the RgbToHex method from the answer by FFire (this version will work correctly for r/g/b values less than 16):

public static String rgbToHex(final int r, final int g, final int b) {
  return "#" + (r < 16 ? "0" : "") + Integer.toHexString(r) + (g < 16 ? "0" : "") +
         Integer.toHexString(g) + (b < 16 ? "0" : "") + Integer.toHexString(b);
}

Of course, you can use a StringBuilder if you wish.

Andy King
  • 1,632
  • 2
  • 20
  • 29