13 Replies Latest reply: Nov 25, 2008 9:49 AM by 843785 RSS

    how do you cast from color to int

    843785
      Color redTransparent=new Color(255,0,0,128);
      image.setRGB(x, y, -8453889 ); works
      insted -8453889 i want color, redTransparent
      but setRGB is (int, int, int)
      getRGB gives an int.
      int redTransparent=new Color(255,0,0,128); does not work.
      how do i convert a color to its int?
        • 1. Re: how do you cast from color to int
          masijade
          setRgb is coordx, coordy, color, getRgb returns only color. Figure it out.
          • 2. Re: how do you cast from color to int
            843785
            Hi,

            1) please use the code tags when submitting code
            2) you cannot cast a Color to an int
            3) ever had a look at
            myColor.getRGB();
            ?

            Bye.
            • 3. Re: how do you cast from color to int
              843785
              i read that color can be defined like
              Color(int red, int green, int blue, int alpha)
              or
              color(int rgb){code}, i needed the value of rgb when i know red, green, blue and alpha.                                                                                                                                                                                                                                                                                                                                                                                                    
              • 4. Re: how do you cast from color to int
                gimbal2
                int rgb = (a << 24) + (r << 16) + (g << 8) + b;
                Without using the Color class at all.
                • 5. Re: how do you cast from color to int
                  843785
                  I am really curious what you are up too.

                  The only reason to be messing around with setRGB is if you are using bufferedimages, which should be obvious since setRGB is a method of buffered images.

                  Yet in your post you seem like you don't really know enough about buffered images to be playing around with it.

                  But what the hey, morbid curiosity is causing me to do something I rarely do, give a complete answer. But I don't think I would turn this method in if I were you, it is short sweet and screems I cheated.
                  private int makeColorAsInt(int alpha, int red, int green, int blue) {
                                return alpha << 24 | red << 16 | green << 8 | blue;
                  } 
                  Still betting you are going in the wrong direction though.

                  JSG
                  • 6. Re: how do you cast from color to int
                    843785
                    i loaded an image into memory, when i move it on the screen with the mouse over other drawings, a certain color of the image must turn into another color and be half transparent.
                    it works but i dont set pixel with the exact color i want,
                    since the graphics program i use gives me the 3 rgb values of the color i select, but setRGB accepts only an integer, and i need that alpha too.
                    thanks for help.
                    • 7. Re: how do you cast from color to int
                      843785
                      Half as transparent as it was, or just half transparent no matter what?

                      Either way you should be set now.

                      JSG
                      • 8. Re: how do you cast from color to int
                        masijade
                        spekulanten wrote:
                        i read that color can be defined like
                        Color(int red, int green, int blue, int alpha)
                        or
                        color(int rgb){code}, i needed the value of rgb when i know red, green, blue and alpha.
                        int rgb = (new Color(r, g, b, a)).getRGB();
                        Where's the problem?
                        • 9. Re: how do you cast from color to int
                          843785
                          i manage to change the color of a color in bitmap,
                          turn blue into red,
                          however couldnt made it transparent, with alpha,
                          so sadly did it with transparent gif.
                          here is the result, a little game.
                          [http://geocities.com/iveseenherinanewfilmlately/dress|http://geocities.com/iveseenherinanewfilmlately/dress]
                          • 10. Re: how do you cast from color to int
                            843785
                            spekulanten wrote:
                            i manage to change the color of a color in bitmap,
                            turn blue into red,
                            however couldnt made it transparent, with alpha,
                            Depends on whether your BufferedImage's (?) ColorModel supports transparency. You could always create one of type TYPE_INT_ARGB
                            • 11. Re: how do you cast from color to int
                              843785
                              Is it possible, viceversa, if i have the color int rgb taken with getRGB from a picture lets say, to find out the red, green, blue of that color, like in Color(r, g, b, a)?
                              • 12. Re: how do you cast from color to int
                                843785
                                spekulanten wrote:
                                Is it possible, viceversa, if i have the color int rgb taken with getRGB from a picture lets say, to find out the red, green, blue of that color, like in Color(r, g, b, a)?
                                Aren't they teaching kids bitwise operations any more?
                                • 13. Re: how do you cast from color to int
                                  masijade
                                  DrLaszloJamf wrote:
                                  spekulanten wrote:
                                  Is it possible, viceversa, if i have the color int rgb taken with getRGB from a picture lets say, to find out the red, green, blue of that color, like in Color(r, g, b, a)?
                                  Aren't they teaching kids bitwise operations any more?
                                  No, many times not, at least not in the basic courses, they have probably moved that to the advanced (if not graduate) courses.

                                  Seemingly, they don't teach them research skills, either (in any course).

                                  @spekulanten: Read the API docs for Color and see if any of those method names jump out you.