Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
arjuna
Posts: 48
Registered: ‎04-07-2011
My Device: 9550

How to Calculate RGB values to get Custom Color

Hi all,

 

 

           How to calculate RGB values to get custom color and draw that custom color on Bitmap.Give me the suggesions or send the Sample Code.

 

 

Thanks & Regards,

Nagarjuna Metla.

arjuna
Developer
PawelGorny
Posts: 150
Registered: ‎12-03-2012
My Device: PP

Re: How to Calculate RGB values to get Custom Color

Check this:

http://sny.no/2011/11/java-hex.html

 

import java.awt.Color;

public class OperaColor extends Color {

  public OperaColor(int r, int g, int b) {
    super(r,g,b);
  }

  /**
   * Returns the HEX value representing the colour in the default sRGB ColorModel.
   *
   * @return the HEX value of the colour in the default sRGB ColorModel
   */
  public String getHex() {
    return toHex(getRed(), getGreen(), getBlue());
  }

  /**
   * Returns a web browser-friendly HEX value representing the colour in the default sRGB
   * ColorModel.
   *
   * @param r red
   * @param g green
   * @param b blue
   * @return a browser-friendly HEX value
   */
  public static String toHex(int r, int g, int b) {
    return "#" + toBrowserHexValue(r) + toBrowserHexValue(g) + toBrowserHexValue(b);
  }

  private static String toBrowserHexValue(int number) {
    StringBuilder builder = new StringBuilder(Integer.toHexString(number & 0xff));
    while (builder.length() < 2) {
      builder.append("0");
    }
    return builder.toString().toUpperCase();
  }

}

 

--
Regards,
Pawel Gorny
my apps: http://pawelgorny.com/
PGpgp, Your Reply, Anagrammatist, Texas Hold'em Odds Calculator, Contacts to CSV
If your issue has been solved, please resolve it by marking "Accept as Solution"
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to Calculate RGB values to get Custom Color

awt is java SE, it does not run on BB.

usually you define colors on a desktop environment, i use paint.net for example, and copy over the rgb code.
you can use "0xhexcode" to define a color for BB.

As far as a color picker goes, i never wrote the one i wanted, but you can find some suggestions about it here:
http://supportforums.blackberry.com/t5/Java-Development/color-picker-existing-code/m-p/79769

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to Calculate RGB values to get Custom Color

I am not clear on what you are asking here, but here goes. 

 

Most paint packages will give you RGB.  There is no need to calculate it. 

 

Colors on BB devices are represented by integers.  If you view this color in hex, it will look like

0xAARRGGBB

where AA is the alpha and RGB is as you would expect.  So

0x00FF0000

is just full on Red.

This is pretty much what the previous post is telling you. 

 

To paint a color onto a Bitmap, you will get the <Graphics> context, and then use

<graphics>.setColor(<custom color>);

then

<graphics>.drawText(...

or

<graphics>.fillRect(...)

 

Hopefully that is enough, if not, then can you ask the question a different way to make it clearer.