Creating random colour in Java?












53















I want to draw random coloured points on a JPanel in a Java application. Is there any method to create random colours?










share|improve this question





























    53















    I want to draw random coloured points on a JPanel in a Java application. Is there any method to create random colours?










    share|improve this question



























      53












      53








      53


      24






      I want to draw random coloured points on a JPanel in a Java application. Is there any method to create random colours?










      share|improve this question
















      I want to draw random coloured points on a JPanel in a Java application. Is there any method to create random colours?







      java colors random






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 19 '15 at 17:59









      Boann

      36.9k1290121




      36.9k1290121










      asked Nov 22 '10 at 14:22









      Elton.fdElton.fd

      71031123




      71031123
























          14 Answers
          14






          active

          oldest

          votes


















          99














          Use the random library:



          import java.util.Random;


          Then create a random generator:



          Random rand = new Random();


          As colours are separated into red green and blue, you can create a new random colour by creating random primary colours:



          // Java 'Color' class takes 3 floats, from 0 to 1.
          float r = rand.nextFloat();
          float g = rand.nextFloat();
          float b = rand.nextFloat();


          Then to finally create the colour, pass the primary colours into the constructor:



          Color randomColor = new Color(r, g, b);




          You can also create different random effects using this method, such as creating random colours with more emphasis on certain colours ... pass in less green and blue to produce a "pinker" random colour.



          // Will produce a random colour with more red in it (usually "pink-ish")
          float r = rand.nextFloat();
          float g = rand.nextFloat() / 2f;
          float b = rand.nextFloat() / 2f;


          Or to ensure that only "light" colours are generated, you can generate colours that are always > 0.5 of each colour element:



          // Will produce only bright / light colours:
          float r = rand.nextFloat() / 2f + 0.5;
          float g = rand.nextFloat() / 2f + 0.5;
          float b = rand.nextFloat() / 2f + 0.5;


          There are various other colour functions that can be used with the Color class, such as making the colour brighter:



          randomColor.brighter();


          An overview of the Color class can be read here: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html






          share|improve this answer





















          • 2





            your code for will only produce bright/light colors will not work. next float does not accept float nor double as an argument and adding .5 makes color go out of range.

            – Xitcod13
            Jun 6 '14 at 14:08













          • Do these colors look good? Do you have a picture?

            – Thomas Ahle
            Jun 27 '14 at 13:52













          • @ThomasAhle It will produce any and every possible colour. I can't draw that in a picture. :)

            – Greg
            Jun 27 '14 at 15:04






          • 1





            As suggested above, doesn't exist anymore a nextFloat constructor that accepts a parameter.

            – giaffa86
            May 8 '15 at 7:24











          • If you want a more "purple" tone of colors what would be the code changes?

            – kabuto178
            Jul 2 '16 at 14:06



















          32














          A one-liner for random RGB values:



          new Color((int)(Math.random() * 0x1000000))





          share|improve this answer































            28














            If you want pleasing, pastel colors, it is best to use the HLS system.



            final float hue = random.nextFloat();
            // Saturation between 0.1 and 0.3
            final float saturation = (random.nextInt(2000) + 1000) / 10000f;
            final float luminance = 0.9f;
            final Color color = Color.getHSBColor(hue, saturation, luminance);





            share|improve this answer



















            • 1





              Do you have a sample with some of the colors this generates?

              – Thomas Ahle
              Jun 27 '14 at 13:53



















            17














            Copy paste this for bright pastel rainbow colors



            int R = (int)(Math.random()*256);
            int G = (int)(Math.random()*256);
            int B= (int)(Math.random()*256);
            Color color = new Color(R, G, B); //random color, but can be bright or dull

            //to get rainbow, pastel colors
            Random random = new Random();
            final float hue = random.nextFloat();
            final float saturation = 0.9f;//1.0 for brilliant, 0.0 for dull
            final float luminance = 1.0f; //1.0 for brighter, 0.0 for black
            color = Color.getHSBColor(hue, saturation, luminance);





            share|improve this answer
























            • Works great, thanks Komplot!

              – Sahil Muthoo
              Jan 27 '12 at 12:04











            • @Komplot: Very nice...

              – Mani
              Jun 16 '14 at 11:26











            • I prefer this way (HSB) of picking a random color. Using the RGB system can create very weird combinations. As a side note, the hue value is internally multiplied by 360 to produce the hue angle in the HSB color model (Javadoc).

              – patrickGranite
              Apr 22 '16 at 14:33





















            9














            If you don't want it to look horrible I'd suggest defining a list of colours in an array and then using a random number generator to pick one.



            If you want a truly random colour you can just generate 3 random numbers from 0 to 255 and then use the Color(int,int,int) constructor to create a new Color instance.



            Random randomGenerator = new Random();
            int red = randomGenerator.nextInt(256);
            int green = randomGenerator.nextInt(256);
            int blue = randomGenerator.nextInt(256);

            Color randomColour = new Color(red,green,blue);





            share|improve this answer





















            • 1





              And this way you can easily avoid points that are the same colour as the background.

              – sje397
              Nov 22 '10 at 14:29





















            5














            I know it's a bit late for this answer, but I've not seen anyone else put this.



            Like Greg said, you want to use the Random class



            Random rand = new Random();


            but the difference I'm going to say is simple do this:



            Color color = new Color(rand.nextInt(0xFFFFFF));


            And it's as simple as that! no need to generate lots of different floats.






            share|improve this answer
























            • Ahh. That's why I must have just skipped over it. The extra colour won't make a difference however :P

              – Shaun Wild
              Feb 11 '14 at 12:34



















            4














            import android.graphics.Color;

            import java.util.Random;

            public class ColorDiagram {
            // Member variables (properties about the object)
            public String mColors = {
            "#39add1", // light blue
            "#3079ab", // dark blue
            "#c25975", // mauve
            "#e15258", // red
            "#f9845b", // orange
            "#838cc7", // lavender
            "#7d669e", // purple
            "#53bbb4", // aqua
            "#51b46d", // green
            "#e0ab18", // mustard
            "#637a91", // dark gray
            "#f092b0", // pink
            "#b7c0c7" // light gray
            };

            // Method (abilities: things the object can do)
            public int getColor() {
            String color = "";

            // Randomly select a fact
            Random randomGenerator = new Random(); // Construct a new Random number generator
            int randomNumber = randomGenerator.nextInt(mColors.length);

            color = mColors[randomNumber];
            int colorAsInt = Color.parseColor(color);

            return colorAsInt;
            }
            }





            share|improve this answer
























            • Thanks, I know how to do this...but I didn't feel like it and you saved me some time!

              – ryanp102694
              Dec 12 '18 at 4:05



















            2














            I have used this simple and clever way for creating random color in Java,



            Random random = new Random();
            System.out.println(String.format("#%06x", random.nextInt(256*256*256)));


            Where #%06x gives you zero-padded hex (always 6 characters long).






            share|improve this answer































              1














              You can instantiate a color with three floats (r, g, b), each between 0.0 and 1.0: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html#Color(float,%20float,%20float).



              Using Java's Random class you can easily instantiate a new random color as such:



              Random r = new Random();
              Color randomColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());


              I can't guarantee they'll all be pretty, but they'll be random =)






              share|improve this answer































                1














                Sure. Just generate a color using random RGB values. Like:



                public Color randomColor()
                {
                Random random=new Random(); // Probably really put this somewhere where it gets executed only once
                int red=random.nextInt(256);
                int green=random.nextInt(256);
                int blue=random.nextInt(256);
                return new Color(red, green, blue);
                }


                You might want to vary up the generation of the random numbers if you don't like the colors it comes up with. I'd guess these will tend to be fairly dark.






                share|improve this answer



















                • 1





                  It's great. but, what can i do for creating lighter color?

                  – Elton.fd
                  Nov 22 '10 at 14:44











                • you can use the Color.brighter() method to make any generated color look like.

                  – Andrew
                  Nov 22 '10 at 14:48






                • 1





                  Sandra, to influence the brightness, make sure the random values are never very dark. 0 is darkest and 255 is brightest, so just do a random.nextInt(128) + 128 for example to never get any colers darker than half brightness.

                  – Stijn de Witt
                  Nov 22 '10 at 14:49











                • yes, It works nice, thanks :)

                  – Elton.fd
                  Nov 22 '10 at 15:03











                • @Stijn: Ditto. I might add that if you want more uniform brightness, you might make the 2nd value depend on the 1st and the 3rd depending on the first two. Like say red=nextInt(255); green=nextInt(255-red); etc. You could play with this sort of thing endlessly until you get the results you want.

                  – Jay
                  Nov 23 '10 at 17:22



















                1














                You seem to want light random colors. Not sure what you mean exactly with light. But if you want random 'rainbow colors', try this



                Random r = new Random();
                Color c = Color.getHSBColor(r.nextFloat(),//random hue, color
                1.0,//full saturation, 1.0 for 'colorful' colors, 0.0 for grey
                1.0 //1.0 for bright, 0.0 for black
                );


                Search for HSB color model for more information.






                share|improve this answer































                  0














                  package com.adil.util;

                  /**
                  * The Class RandomColor.
                  *
                  * @author Adil OUIDAD
                  * @URL : http://kizana.fr
                  */
                  public class RandomColor {
                  /**
                  * Gets the random color.
                  *
                  * @return the random color
                  */
                  public static String getRandomColor() {
                  String letters = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
                  String color = "#";
                  for (int i = 0; i < 6; i++ ) {
                  color += letters[(int) Math.round(Math.random() * 15)];
                  }
                  return color;
                  }
                  }





                  share|improve this answer

































                    0














                    Here is a method for getting a random color:



                    private static Random sRandom;

                    public static synchronized int randomColor() {
                    if (sRandom == null) {
                    sRandom = new Random();
                    }
                    return 0xff000000 + 256 * 256 * sRandom.nextInt(256) + 256 * sRandom.nextInt(256)
                    + sRandom.nextInt(256);
                    }


                    Benefits:




                    • Get the integer representation which can be used with java.awt.Color or android.graphics.Color

                    • Keep a static reference to Random.






                    share|improve this answer

































                      -1














                      It would be helpful.



                      Random randomGenerator = new Random();
                      int red = randomGenerator.nextInt(255);
                      int green = randomGenerator.nextInt(255);
                      int blue = randomGenerator.nextInt(255);
                      Color randomColour = new Color(red,green,blue);





                      share|improve this answer


























                      • All you did was copy-paste the other answer.

                        – Boann
                        Dec 3 '14 at 13:46











                      • I tried this..It is working..that's whay I prefer this..

                        – K.Mahesh
                        Dec 4 '14 at 5:10











                      • Oh I see. That's not what we do on Stack Overflow. The thing to do is earn 15 rep on the site (by posting new useful questions and answers), so you can vote up the answers you find useful.

                        – Boann
                        Dec 4 '14 at 13:01











                      • Okay..Next time I'll focus on that.

                        – K.Mahesh
                        Dec 5 '14 at 5:45











                      Your Answer






                      StackExchange.ifUsing("editor", function () {
                      StackExchange.using("externalEditor", function () {
                      StackExchange.using("snippets", function () {
                      StackExchange.snippets.init();
                      });
                      });
                      }, "code-snippets");

                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "1"
                      };
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function() {
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled) {
                      StackExchange.using("snippets", function() {
                      createEditor();
                      });
                      }
                      else {
                      createEditor();
                      }
                      });

                      function createEditor() {
                      StackExchange.prepareEditor({
                      heartbeatType: 'answer',
                      autoActivateHeartbeat: false,
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });














                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4246351%2fcreating-random-colour-in-java%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      14 Answers
                      14






                      active

                      oldest

                      votes








                      14 Answers
                      14






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      99














                      Use the random library:



                      import java.util.Random;


                      Then create a random generator:



                      Random rand = new Random();


                      As colours are separated into red green and blue, you can create a new random colour by creating random primary colours:



                      // Java 'Color' class takes 3 floats, from 0 to 1.
                      float r = rand.nextFloat();
                      float g = rand.nextFloat();
                      float b = rand.nextFloat();


                      Then to finally create the colour, pass the primary colours into the constructor:



                      Color randomColor = new Color(r, g, b);




                      You can also create different random effects using this method, such as creating random colours with more emphasis on certain colours ... pass in less green and blue to produce a "pinker" random colour.



                      // Will produce a random colour with more red in it (usually "pink-ish")
                      float r = rand.nextFloat();
                      float g = rand.nextFloat() / 2f;
                      float b = rand.nextFloat() / 2f;


                      Or to ensure that only "light" colours are generated, you can generate colours that are always > 0.5 of each colour element:



                      // Will produce only bright / light colours:
                      float r = rand.nextFloat() / 2f + 0.5;
                      float g = rand.nextFloat() / 2f + 0.5;
                      float b = rand.nextFloat() / 2f + 0.5;


                      There are various other colour functions that can be used with the Color class, such as making the colour brighter:



                      randomColor.brighter();


                      An overview of the Color class can be read here: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html






                      share|improve this answer





















                      • 2





                        your code for will only produce bright/light colors will not work. next float does not accept float nor double as an argument and adding .5 makes color go out of range.

                        – Xitcod13
                        Jun 6 '14 at 14:08













                      • Do these colors look good? Do you have a picture?

                        – Thomas Ahle
                        Jun 27 '14 at 13:52













                      • @ThomasAhle It will produce any and every possible colour. I can't draw that in a picture. :)

                        – Greg
                        Jun 27 '14 at 15:04






                      • 1





                        As suggested above, doesn't exist anymore a nextFloat constructor that accepts a parameter.

                        – giaffa86
                        May 8 '15 at 7:24











                      • If you want a more "purple" tone of colors what would be the code changes?

                        – kabuto178
                        Jul 2 '16 at 14:06
















                      99














                      Use the random library:



                      import java.util.Random;


                      Then create a random generator:



                      Random rand = new Random();


                      As colours are separated into red green and blue, you can create a new random colour by creating random primary colours:



                      // Java 'Color' class takes 3 floats, from 0 to 1.
                      float r = rand.nextFloat();
                      float g = rand.nextFloat();
                      float b = rand.nextFloat();


                      Then to finally create the colour, pass the primary colours into the constructor:



                      Color randomColor = new Color(r, g, b);




                      You can also create different random effects using this method, such as creating random colours with more emphasis on certain colours ... pass in less green and blue to produce a "pinker" random colour.



                      // Will produce a random colour with more red in it (usually "pink-ish")
                      float r = rand.nextFloat();
                      float g = rand.nextFloat() / 2f;
                      float b = rand.nextFloat() / 2f;


                      Or to ensure that only "light" colours are generated, you can generate colours that are always > 0.5 of each colour element:



                      // Will produce only bright / light colours:
                      float r = rand.nextFloat() / 2f + 0.5;
                      float g = rand.nextFloat() / 2f + 0.5;
                      float b = rand.nextFloat() / 2f + 0.5;


                      There are various other colour functions that can be used with the Color class, such as making the colour brighter:



                      randomColor.brighter();


                      An overview of the Color class can be read here: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html






                      share|improve this answer





















                      • 2





                        your code for will only produce bright/light colors will not work. next float does not accept float nor double as an argument and adding .5 makes color go out of range.

                        – Xitcod13
                        Jun 6 '14 at 14:08













                      • Do these colors look good? Do you have a picture?

                        – Thomas Ahle
                        Jun 27 '14 at 13:52













                      • @ThomasAhle It will produce any and every possible colour. I can't draw that in a picture. :)

                        – Greg
                        Jun 27 '14 at 15:04






                      • 1





                        As suggested above, doesn't exist anymore a nextFloat constructor that accepts a parameter.

                        – giaffa86
                        May 8 '15 at 7:24











                      • If you want a more "purple" tone of colors what would be the code changes?

                        – kabuto178
                        Jul 2 '16 at 14:06














                      99












                      99








                      99







                      Use the random library:



                      import java.util.Random;


                      Then create a random generator:



                      Random rand = new Random();


                      As colours are separated into red green and blue, you can create a new random colour by creating random primary colours:



                      // Java 'Color' class takes 3 floats, from 0 to 1.
                      float r = rand.nextFloat();
                      float g = rand.nextFloat();
                      float b = rand.nextFloat();


                      Then to finally create the colour, pass the primary colours into the constructor:



                      Color randomColor = new Color(r, g, b);




                      You can also create different random effects using this method, such as creating random colours with more emphasis on certain colours ... pass in less green and blue to produce a "pinker" random colour.



                      // Will produce a random colour with more red in it (usually "pink-ish")
                      float r = rand.nextFloat();
                      float g = rand.nextFloat() / 2f;
                      float b = rand.nextFloat() / 2f;


                      Or to ensure that only "light" colours are generated, you can generate colours that are always > 0.5 of each colour element:



                      // Will produce only bright / light colours:
                      float r = rand.nextFloat() / 2f + 0.5;
                      float g = rand.nextFloat() / 2f + 0.5;
                      float b = rand.nextFloat() / 2f + 0.5;


                      There are various other colour functions that can be used with the Color class, such as making the colour brighter:



                      randomColor.brighter();


                      An overview of the Color class can be read here: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html






                      share|improve this answer















                      Use the random library:



                      import java.util.Random;


                      Then create a random generator:



                      Random rand = new Random();


                      As colours are separated into red green and blue, you can create a new random colour by creating random primary colours:



                      // Java 'Color' class takes 3 floats, from 0 to 1.
                      float r = rand.nextFloat();
                      float g = rand.nextFloat();
                      float b = rand.nextFloat();


                      Then to finally create the colour, pass the primary colours into the constructor:



                      Color randomColor = new Color(r, g, b);




                      You can also create different random effects using this method, such as creating random colours with more emphasis on certain colours ... pass in less green and blue to produce a "pinker" random colour.



                      // Will produce a random colour with more red in it (usually "pink-ish")
                      float r = rand.nextFloat();
                      float g = rand.nextFloat() / 2f;
                      float b = rand.nextFloat() / 2f;


                      Or to ensure that only "light" colours are generated, you can generate colours that are always > 0.5 of each colour element:



                      // Will produce only bright / light colours:
                      float r = rand.nextFloat() / 2f + 0.5;
                      float g = rand.nextFloat() / 2f + 0.5;
                      float b = rand.nextFloat() / 2f + 0.5;


                      There are various other colour functions that can be used with the Color class, such as making the colour brighter:



                      randomColor.brighter();


                      An overview of the Color class can be read here: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 24 '15 at 18:10









                      Panda TG Attwood

                      593824




                      593824










                      answered Nov 22 '10 at 14:28









                      GregGreg

                      15.1k106693




                      15.1k106693








                      • 2





                        your code for will only produce bright/light colors will not work. next float does not accept float nor double as an argument and adding .5 makes color go out of range.

                        – Xitcod13
                        Jun 6 '14 at 14:08













                      • Do these colors look good? Do you have a picture?

                        – Thomas Ahle
                        Jun 27 '14 at 13:52













                      • @ThomasAhle It will produce any and every possible colour. I can't draw that in a picture. :)

                        – Greg
                        Jun 27 '14 at 15:04






                      • 1





                        As suggested above, doesn't exist anymore a nextFloat constructor that accepts a parameter.

                        – giaffa86
                        May 8 '15 at 7:24











                      • If you want a more "purple" tone of colors what would be the code changes?

                        – kabuto178
                        Jul 2 '16 at 14:06














                      • 2





                        your code for will only produce bright/light colors will not work. next float does not accept float nor double as an argument and adding .5 makes color go out of range.

                        – Xitcod13
                        Jun 6 '14 at 14:08













                      • Do these colors look good? Do you have a picture?

                        – Thomas Ahle
                        Jun 27 '14 at 13:52













                      • @ThomasAhle It will produce any and every possible colour. I can't draw that in a picture. :)

                        – Greg
                        Jun 27 '14 at 15:04






                      • 1





                        As suggested above, doesn't exist anymore a nextFloat constructor that accepts a parameter.

                        – giaffa86
                        May 8 '15 at 7:24











                      • If you want a more "purple" tone of colors what would be the code changes?

                        – kabuto178
                        Jul 2 '16 at 14:06








                      2




                      2





                      your code for will only produce bright/light colors will not work. next float does not accept float nor double as an argument and adding .5 makes color go out of range.

                      – Xitcod13
                      Jun 6 '14 at 14:08







                      your code for will only produce bright/light colors will not work. next float does not accept float nor double as an argument and adding .5 makes color go out of range.

                      – Xitcod13
                      Jun 6 '14 at 14:08















                      Do these colors look good? Do you have a picture?

                      – Thomas Ahle
                      Jun 27 '14 at 13:52







                      Do these colors look good? Do you have a picture?

                      – Thomas Ahle
                      Jun 27 '14 at 13:52















                      @ThomasAhle It will produce any and every possible colour. I can't draw that in a picture. :)

                      – Greg
                      Jun 27 '14 at 15:04





                      @ThomasAhle It will produce any and every possible colour. I can't draw that in a picture. :)

                      – Greg
                      Jun 27 '14 at 15:04




                      1




                      1





                      As suggested above, doesn't exist anymore a nextFloat constructor that accepts a parameter.

                      – giaffa86
                      May 8 '15 at 7:24





                      As suggested above, doesn't exist anymore a nextFloat constructor that accepts a parameter.

                      – giaffa86
                      May 8 '15 at 7:24













                      If you want a more "purple" tone of colors what would be the code changes?

                      – kabuto178
                      Jul 2 '16 at 14:06





                      If you want a more "purple" tone of colors what would be the code changes?

                      – kabuto178
                      Jul 2 '16 at 14:06













                      32














                      A one-liner for random RGB values:



                      new Color((int)(Math.random() * 0x1000000))





                      share|improve this answer




























                        32














                        A one-liner for random RGB values:



                        new Color((int)(Math.random() * 0x1000000))





                        share|improve this answer


























                          32












                          32








                          32







                          A one-liner for random RGB values:



                          new Color((int)(Math.random() * 0x1000000))





                          share|improve this answer













                          A one-liner for random RGB values:



                          new Color((int)(Math.random() * 0x1000000))






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 17 '13 at 14:46









                          BoannBoann

                          36.9k1290121




                          36.9k1290121























                              28














                              If you want pleasing, pastel colors, it is best to use the HLS system.



                              final float hue = random.nextFloat();
                              // Saturation between 0.1 and 0.3
                              final float saturation = (random.nextInt(2000) + 1000) / 10000f;
                              final float luminance = 0.9f;
                              final Color color = Color.getHSBColor(hue, saturation, luminance);





                              share|improve this answer



















                              • 1





                                Do you have a sample with some of the colors this generates?

                                – Thomas Ahle
                                Jun 27 '14 at 13:53
















                              28














                              If you want pleasing, pastel colors, it is best to use the HLS system.



                              final float hue = random.nextFloat();
                              // Saturation between 0.1 and 0.3
                              final float saturation = (random.nextInt(2000) + 1000) / 10000f;
                              final float luminance = 0.9f;
                              final Color color = Color.getHSBColor(hue, saturation, luminance);





                              share|improve this answer



















                              • 1





                                Do you have a sample with some of the colors this generates?

                                – Thomas Ahle
                                Jun 27 '14 at 13:53














                              28












                              28








                              28







                              If you want pleasing, pastel colors, it is best to use the HLS system.



                              final float hue = random.nextFloat();
                              // Saturation between 0.1 and 0.3
                              final float saturation = (random.nextInt(2000) + 1000) / 10000f;
                              final float luminance = 0.9f;
                              final Color color = Color.getHSBColor(hue, saturation, luminance);





                              share|improve this answer













                              If you want pleasing, pastel colors, it is best to use the HLS system.



                              final float hue = random.nextFloat();
                              // Saturation between 0.1 and 0.3
                              final float saturation = (random.nextInt(2000) + 1000) / 10000f;
                              final float luminance = 0.9f;
                              final Color color = Color.getHSBColor(hue, saturation, luminance);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 '10 at 15:45









                              Sualeh FatehiSualeh Fatehi

                              3,75011625




                              3,75011625








                              • 1





                                Do you have a sample with some of the colors this generates?

                                – Thomas Ahle
                                Jun 27 '14 at 13:53














                              • 1





                                Do you have a sample with some of the colors this generates?

                                – Thomas Ahle
                                Jun 27 '14 at 13:53








                              1




                              1





                              Do you have a sample with some of the colors this generates?

                              – Thomas Ahle
                              Jun 27 '14 at 13:53





                              Do you have a sample with some of the colors this generates?

                              – Thomas Ahle
                              Jun 27 '14 at 13:53











                              17














                              Copy paste this for bright pastel rainbow colors



                              int R = (int)(Math.random()*256);
                              int G = (int)(Math.random()*256);
                              int B= (int)(Math.random()*256);
                              Color color = new Color(R, G, B); //random color, but can be bright or dull

                              //to get rainbow, pastel colors
                              Random random = new Random();
                              final float hue = random.nextFloat();
                              final float saturation = 0.9f;//1.0 for brilliant, 0.0 for dull
                              final float luminance = 1.0f; //1.0 for brighter, 0.0 for black
                              color = Color.getHSBColor(hue, saturation, luminance);





                              share|improve this answer
























                              • Works great, thanks Komplot!

                                – Sahil Muthoo
                                Jan 27 '12 at 12:04











                              • @Komplot: Very nice...

                                – Mani
                                Jun 16 '14 at 11:26











                              • I prefer this way (HSB) of picking a random color. Using the RGB system can create very weird combinations. As a side note, the hue value is internally multiplied by 360 to produce the hue angle in the HSB color model (Javadoc).

                                – patrickGranite
                                Apr 22 '16 at 14:33


















                              17














                              Copy paste this for bright pastel rainbow colors



                              int R = (int)(Math.random()*256);
                              int G = (int)(Math.random()*256);
                              int B= (int)(Math.random()*256);
                              Color color = new Color(R, G, B); //random color, but can be bright or dull

                              //to get rainbow, pastel colors
                              Random random = new Random();
                              final float hue = random.nextFloat();
                              final float saturation = 0.9f;//1.0 for brilliant, 0.0 for dull
                              final float luminance = 1.0f; //1.0 for brighter, 0.0 for black
                              color = Color.getHSBColor(hue, saturation, luminance);





                              share|improve this answer
























                              • Works great, thanks Komplot!

                                – Sahil Muthoo
                                Jan 27 '12 at 12:04











                              • @Komplot: Very nice...

                                – Mani
                                Jun 16 '14 at 11:26











                              • I prefer this way (HSB) of picking a random color. Using the RGB system can create very weird combinations. As a side note, the hue value is internally multiplied by 360 to produce the hue angle in the HSB color model (Javadoc).

                                – patrickGranite
                                Apr 22 '16 at 14:33
















                              17












                              17








                              17







                              Copy paste this for bright pastel rainbow colors



                              int R = (int)(Math.random()*256);
                              int G = (int)(Math.random()*256);
                              int B= (int)(Math.random()*256);
                              Color color = new Color(R, G, B); //random color, but can be bright or dull

                              //to get rainbow, pastel colors
                              Random random = new Random();
                              final float hue = random.nextFloat();
                              final float saturation = 0.9f;//1.0 for brilliant, 0.0 for dull
                              final float luminance = 1.0f; //1.0 for brighter, 0.0 for black
                              color = Color.getHSBColor(hue, saturation, luminance);





                              share|improve this answer













                              Copy paste this for bright pastel rainbow colors



                              int R = (int)(Math.random()*256);
                              int G = (int)(Math.random()*256);
                              int B= (int)(Math.random()*256);
                              Color color = new Color(R, G, B); //random color, but can be bright or dull

                              //to get rainbow, pastel colors
                              Random random = new Random();
                              final float hue = random.nextFloat();
                              final float saturation = 0.9f;//1.0 for brilliant, 0.0 for dull
                              final float luminance = 1.0f; //1.0 for brighter, 0.0 for black
                              color = Color.getHSBColor(hue, saturation, luminance);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 5 '12 at 7:54









                              KomplotKomplot

                              44647




                              44647













                              • Works great, thanks Komplot!

                                – Sahil Muthoo
                                Jan 27 '12 at 12:04











                              • @Komplot: Very nice...

                                – Mani
                                Jun 16 '14 at 11:26











                              • I prefer this way (HSB) of picking a random color. Using the RGB system can create very weird combinations. As a side note, the hue value is internally multiplied by 360 to produce the hue angle in the HSB color model (Javadoc).

                                – patrickGranite
                                Apr 22 '16 at 14:33





















                              • Works great, thanks Komplot!

                                – Sahil Muthoo
                                Jan 27 '12 at 12:04











                              • @Komplot: Very nice...

                                – Mani
                                Jun 16 '14 at 11:26











                              • I prefer this way (HSB) of picking a random color. Using the RGB system can create very weird combinations. As a side note, the hue value is internally multiplied by 360 to produce the hue angle in the HSB color model (Javadoc).

                                – patrickGranite
                                Apr 22 '16 at 14:33



















                              Works great, thanks Komplot!

                              – Sahil Muthoo
                              Jan 27 '12 at 12:04





                              Works great, thanks Komplot!

                              – Sahil Muthoo
                              Jan 27 '12 at 12:04













                              @Komplot: Very nice...

                              – Mani
                              Jun 16 '14 at 11:26





                              @Komplot: Very nice...

                              – Mani
                              Jun 16 '14 at 11:26













                              I prefer this way (HSB) of picking a random color. Using the RGB system can create very weird combinations. As a side note, the hue value is internally multiplied by 360 to produce the hue angle in the HSB color model (Javadoc).

                              – patrickGranite
                              Apr 22 '16 at 14:33







                              I prefer this way (HSB) of picking a random color. Using the RGB system can create very weird combinations. As a side note, the hue value is internally multiplied by 360 to produce the hue angle in the HSB color model (Javadoc).

                              – patrickGranite
                              Apr 22 '16 at 14:33













                              9














                              If you don't want it to look horrible I'd suggest defining a list of colours in an array and then using a random number generator to pick one.



                              If you want a truly random colour you can just generate 3 random numbers from 0 to 255 and then use the Color(int,int,int) constructor to create a new Color instance.



                              Random randomGenerator = new Random();
                              int red = randomGenerator.nextInt(256);
                              int green = randomGenerator.nextInt(256);
                              int blue = randomGenerator.nextInt(256);

                              Color randomColour = new Color(red,green,blue);





                              share|improve this answer





















                              • 1





                                And this way you can easily avoid points that are the same colour as the background.

                                – sje397
                                Nov 22 '10 at 14:29


















                              9














                              If you don't want it to look horrible I'd suggest defining a list of colours in an array and then using a random number generator to pick one.



                              If you want a truly random colour you can just generate 3 random numbers from 0 to 255 and then use the Color(int,int,int) constructor to create a new Color instance.



                              Random randomGenerator = new Random();
                              int red = randomGenerator.nextInt(256);
                              int green = randomGenerator.nextInt(256);
                              int blue = randomGenerator.nextInt(256);

                              Color randomColour = new Color(red,green,blue);





                              share|improve this answer





















                              • 1





                                And this way you can easily avoid points that are the same colour as the background.

                                – sje397
                                Nov 22 '10 at 14:29
















                              9












                              9








                              9







                              If you don't want it to look horrible I'd suggest defining a list of colours in an array and then using a random number generator to pick one.



                              If you want a truly random colour you can just generate 3 random numbers from 0 to 255 and then use the Color(int,int,int) constructor to create a new Color instance.



                              Random randomGenerator = new Random();
                              int red = randomGenerator.nextInt(256);
                              int green = randomGenerator.nextInt(256);
                              int blue = randomGenerator.nextInt(256);

                              Color randomColour = new Color(red,green,blue);





                              share|improve this answer















                              If you don't want it to look horrible I'd suggest defining a list of colours in an array and then using a random number generator to pick one.



                              If you want a truly random colour you can just generate 3 random numbers from 0 to 255 and then use the Color(int,int,int) constructor to create a new Color instance.



                              Random randomGenerator = new Random();
                              int red = randomGenerator.nextInt(256);
                              int green = randomGenerator.nextInt(256);
                              int blue = randomGenerator.nextInt(256);

                              Color randomColour = new Color(red,green,blue);






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 22 '14 at 4:12









                              Boann

                              36.9k1290121




                              36.9k1290121










                              answered Nov 22 '10 at 14:26









                              Rob Stevenson-LeggettRob Stevenson-Leggett

                              21.8k1779134




                              21.8k1779134








                              • 1





                                And this way you can easily avoid points that are the same colour as the background.

                                – sje397
                                Nov 22 '10 at 14:29
















                              • 1





                                And this way you can easily avoid points that are the same colour as the background.

                                – sje397
                                Nov 22 '10 at 14:29










                              1




                              1





                              And this way you can easily avoid points that are the same colour as the background.

                              – sje397
                              Nov 22 '10 at 14:29







                              And this way you can easily avoid points that are the same colour as the background.

                              – sje397
                              Nov 22 '10 at 14:29













                              5














                              I know it's a bit late for this answer, but I've not seen anyone else put this.



                              Like Greg said, you want to use the Random class



                              Random rand = new Random();


                              but the difference I'm going to say is simple do this:



                              Color color = new Color(rand.nextInt(0xFFFFFF));


                              And it's as simple as that! no need to generate lots of different floats.






                              share|improve this answer
























                              • Ahh. That's why I must have just skipped over it. The extra colour won't make a difference however :P

                                – Shaun Wild
                                Feb 11 '14 at 12:34
















                              5














                              I know it's a bit late for this answer, but I've not seen anyone else put this.



                              Like Greg said, you want to use the Random class



                              Random rand = new Random();


                              but the difference I'm going to say is simple do this:



                              Color color = new Color(rand.nextInt(0xFFFFFF));


                              And it's as simple as that! no need to generate lots of different floats.






                              share|improve this answer
























                              • Ahh. That's why I must have just skipped over it. The extra colour won't make a difference however :P

                                – Shaun Wild
                                Feb 11 '14 at 12:34














                              5












                              5








                              5







                              I know it's a bit late for this answer, but I've not seen anyone else put this.



                              Like Greg said, you want to use the Random class



                              Random rand = new Random();


                              but the difference I'm going to say is simple do this:



                              Color color = new Color(rand.nextInt(0xFFFFFF));


                              And it's as simple as that! no need to generate lots of different floats.






                              share|improve this answer













                              I know it's a bit late for this answer, but I've not seen anyone else put this.



                              Like Greg said, you want to use the Random class



                              Random rand = new Random();


                              but the difference I'm going to say is simple do this:



                              Color color = new Color(rand.nextInt(0xFFFFFF));


                              And it's as simple as that! no need to generate lots of different floats.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 11 '14 at 11:31









                              Shaun WildShaun Wild

                              63921130




                              63921130













                              • Ahh. That's why I must have just skipped over it. The extra colour won't make a difference however :P

                                – Shaun Wild
                                Feb 11 '14 at 12:34



















                              • Ahh. That's why I must have just skipped over it. The extra colour won't make a difference however :P

                                – Shaun Wild
                                Feb 11 '14 at 12:34

















                              Ahh. That's why I must have just skipped over it. The extra colour won't make a difference however :P

                              – Shaun Wild
                              Feb 11 '14 at 12:34





                              Ahh. That's why I must have just skipped over it. The extra colour won't make a difference however :P

                              – Shaun Wild
                              Feb 11 '14 at 12:34











                              4














                              import android.graphics.Color;

                              import java.util.Random;

                              public class ColorDiagram {
                              // Member variables (properties about the object)
                              public String mColors = {
                              "#39add1", // light blue
                              "#3079ab", // dark blue
                              "#c25975", // mauve
                              "#e15258", // red
                              "#f9845b", // orange
                              "#838cc7", // lavender
                              "#7d669e", // purple
                              "#53bbb4", // aqua
                              "#51b46d", // green
                              "#e0ab18", // mustard
                              "#637a91", // dark gray
                              "#f092b0", // pink
                              "#b7c0c7" // light gray
                              };

                              // Method (abilities: things the object can do)
                              public int getColor() {
                              String color = "";

                              // Randomly select a fact
                              Random randomGenerator = new Random(); // Construct a new Random number generator
                              int randomNumber = randomGenerator.nextInt(mColors.length);

                              color = mColors[randomNumber];
                              int colorAsInt = Color.parseColor(color);

                              return colorAsInt;
                              }
                              }





                              share|improve this answer
























                              • Thanks, I know how to do this...but I didn't feel like it and you saved me some time!

                                – ryanp102694
                                Dec 12 '18 at 4:05
















                              4














                              import android.graphics.Color;

                              import java.util.Random;

                              public class ColorDiagram {
                              // Member variables (properties about the object)
                              public String mColors = {
                              "#39add1", // light blue
                              "#3079ab", // dark blue
                              "#c25975", // mauve
                              "#e15258", // red
                              "#f9845b", // orange
                              "#838cc7", // lavender
                              "#7d669e", // purple
                              "#53bbb4", // aqua
                              "#51b46d", // green
                              "#e0ab18", // mustard
                              "#637a91", // dark gray
                              "#f092b0", // pink
                              "#b7c0c7" // light gray
                              };

                              // Method (abilities: things the object can do)
                              public int getColor() {
                              String color = "";

                              // Randomly select a fact
                              Random randomGenerator = new Random(); // Construct a new Random number generator
                              int randomNumber = randomGenerator.nextInt(mColors.length);

                              color = mColors[randomNumber];
                              int colorAsInt = Color.parseColor(color);

                              return colorAsInt;
                              }
                              }





                              share|improve this answer
























                              • Thanks, I know how to do this...but I didn't feel like it and you saved me some time!

                                – ryanp102694
                                Dec 12 '18 at 4:05














                              4












                              4








                              4







                              import android.graphics.Color;

                              import java.util.Random;

                              public class ColorDiagram {
                              // Member variables (properties about the object)
                              public String mColors = {
                              "#39add1", // light blue
                              "#3079ab", // dark blue
                              "#c25975", // mauve
                              "#e15258", // red
                              "#f9845b", // orange
                              "#838cc7", // lavender
                              "#7d669e", // purple
                              "#53bbb4", // aqua
                              "#51b46d", // green
                              "#e0ab18", // mustard
                              "#637a91", // dark gray
                              "#f092b0", // pink
                              "#b7c0c7" // light gray
                              };

                              // Method (abilities: things the object can do)
                              public int getColor() {
                              String color = "";

                              // Randomly select a fact
                              Random randomGenerator = new Random(); // Construct a new Random number generator
                              int randomNumber = randomGenerator.nextInt(mColors.length);

                              color = mColors[randomNumber];
                              int colorAsInt = Color.parseColor(color);

                              return colorAsInt;
                              }
                              }





                              share|improve this answer













                              import android.graphics.Color;

                              import java.util.Random;

                              public class ColorDiagram {
                              // Member variables (properties about the object)
                              public String mColors = {
                              "#39add1", // light blue
                              "#3079ab", // dark blue
                              "#c25975", // mauve
                              "#e15258", // red
                              "#f9845b", // orange
                              "#838cc7", // lavender
                              "#7d669e", // purple
                              "#53bbb4", // aqua
                              "#51b46d", // green
                              "#e0ab18", // mustard
                              "#637a91", // dark gray
                              "#f092b0", // pink
                              "#b7c0c7" // light gray
                              };

                              // Method (abilities: things the object can do)
                              public int getColor() {
                              String color = "";

                              // Randomly select a fact
                              Random randomGenerator = new Random(); // Construct a new Random number generator
                              int randomNumber = randomGenerator.nextInt(mColors.length);

                              color = mColors[randomNumber];
                              int colorAsInt = Color.parseColor(color);

                              return colorAsInt;
                              }
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 28 '15 at 16:45









                              Durul DalkanatDurul Dalkanat

                              4,81622732




                              4,81622732













                              • Thanks, I know how to do this...but I didn't feel like it and you saved me some time!

                                – ryanp102694
                                Dec 12 '18 at 4:05



















                              • Thanks, I know how to do this...but I didn't feel like it and you saved me some time!

                                – ryanp102694
                                Dec 12 '18 at 4:05

















                              Thanks, I know how to do this...but I didn't feel like it and you saved me some time!

                              – ryanp102694
                              Dec 12 '18 at 4:05





                              Thanks, I know how to do this...but I didn't feel like it and you saved me some time!

                              – ryanp102694
                              Dec 12 '18 at 4:05











                              2














                              I have used this simple and clever way for creating random color in Java,



                              Random random = new Random();
                              System.out.println(String.format("#%06x", random.nextInt(256*256*256)));


                              Where #%06x gives you zero-padded hex (always 6 characters long).






                              share|improve this answer




























                                2














                                I have used this simple and clever way for creating random color in Java,



                                Random random = new Random();
                                System.out.println(String.format("#%06x", random.nextInt(256*256*256)));


                                Where #%06x gives you zero-padded hex (always 6 characters long).






                                share|improve this answer


























                                  2












                                  2








                                  2







                                  I have used this simple and clever way for creating random color in Java,



                                  Random random = new Random();
                                  System.out.println(String.format("#%06x", random.nextInt(256*256*256)));


                                  Where #%06x gives you zero-padded hex (always 6 characters long).






                                  share|improve this answer













                                  I have used this simple and clever way for creating random color in Java,



                                  Random random = new Random();
                                  System.out.println(String.format("#%06x", random.nextInt(256*256*256)));


                                  Where #%06x gives you zero-padded hex (always 6 characters long).







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jan 12 '18 at 13:31









                                  Deepak KumbharDeepak Kumbhar

                                  131210




                                  131210























                                      1














                                      You can instantiate a color with three floats (r, g, b), each between 0.0 and 1.0: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html#Color(float,%20float,%20float).



                                      Using Java's Random class you can easily instantiate a new random color as such:



                                      Random r = new Random();
                                      Color randomColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());


                                      I can't guarantee they'll all be pretty, but they'll be random =)






                                      share|improve this answer




























                                        1














                                        You can instantiate a color with three floats (r, g, b), each between 0.0 and 1.0: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html#Color(float,%20float,%20float).



                                        Using Java's Random class you can easily instantiate a new random color as such:



                                        Random r = new Random();
                                        Color randomColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());


                                        I can't guarantee they'll all be pretty, but they'll be random =)






                                        share|improve this answer


























                                          1












                                          1








                                          1







                                          You can instantiate a color with three floats (r, g, b), each between 0.0 and 1.0: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html#Color(float,%20float,%20float).



                                          Using Java's Random class you can easily instantiate a new random color as such:



                                          Random r = new Random();
                                          Color randomColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());


                                          I can't guarantee they'll all be pretty, but they'll be random =)






                                          share|improve this answer













                                          You can instantiate a color with three floats (r, g, b), each between 0.0 and 1.0: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html#Color(float,%20float,%20float).



                                          Using Java's Random class you can easily instantiate a new random color as such:



                                          Random r = new Random();
                                          Color randomColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());


                                          I can't guarantee they'll all be pretty, but they'll be random =)







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Nov 22 '10 at 14:27









                                          Jason NicholsJason Nichols

                                          9,91642950




                                          9,91642950























                                              1














                                              Sure. Just generate a color using random RGB values. Like:



                                              public Color randomColor()
                                              {
                                              Random random=new Random(); // Probably really put this somewhere where it gets executed only once
                                              int red=random.nextInt(256);
                                              int green=random.nextInt(256);
                                              int blue=random.nextInt(256);
                                              return new Color(red, green, blue);
                                              }


                                              You might want to vary up the generation of the random numbers if you don't like the colors it comes up with. I'd guess these will tend to be fairly dark.






                                              share|improve this answer



















                                              • 1





                                                It's great. but, what can i do for creating lighter color?

                                                – Elton.fd
                                                Nov 22 '10 at 14:44











                                              • you can use the Color.brighter() method to make any generated color look like.

                                                – Andrew
                                                Nov 22 '10 at 14:48






                                              • 1





                                                Sandra, to influence the brightness, make sure the random values are never very dark. 0 is darkest and 255 is brightest, so just do a random.nextInt(128) + 128 for example to never get any colers darker than half brightness.

                                                – Stijn de Witt
                                                Nov 22 '10 at 14:49











                                              • yes, It works nice, thanks :)

                                                – Elton.fd
                                                Nov 22 '10 at 15:03











                                              • @Stijn: Ditto. I might add that if you want more uniform brightness, you might make the 2nd value depend on the 1st and the 3rd depending on the first two. Like say red=nextInt(255); green=nextInt(255-red); etc. You could play with this sort of thing endlessly until you get the results you want.

                                                – Jay
                                                Nov 23 '10 at 17:22
















                                              1














                                              Sure. Just generate a color using random RGB values. Like:



                                              public Color randomColor()
                                              {
                                              Random random=new Random(); // Probably really put this somewhere where it gets executed only once
                                              int red=random.nextInt(256);
                                              int green=random.nextInt(256);
                                              int blue=random.nextInt(256);
                                              return new Color(red, green, blue);
                                              }


                                              You might want to vary up the generation of the random numbers if you don't like the colors it comes up with. I'd guess these will tend to be fairly dark.






                                              share|improve this answer



















                                              • 1





                                                It's great. but, what can i do for creating lighter color?

                                                – Elton.fd
                                                Nov 22 '10 at 14:44











                                              • you can use the Color.brighter() method to make any generated color look like.

                                                – Andrew
                                                Nov 22 '10 at 14:48






                                              • 1





                                                Sandra, to influence the brightness, make sure the random values are never very dark. 0 is darkest and 255 is brightest, so just do a random.nextInt(128) + 128 for example to never get any colers darker than half brightness.

                                                – Stijn de Witt
                                                Nov 22 '10 at 14:49











                                              • yes, It works nice, thanks :)

                                                – Elton.fd
                                                Nov 22 '10 at 15:03











                                              • @Stijn: Ditto. I might add that if you want more uniform brightness, you might make the 2nd value depend on the 1st and the 3rd depending on the first two. Like say red=nextInt(255); green=nextInt(255-red); etc. You could play with this sort of thing endlessly until you get the results you want.

                                                – Jay
                                                Nov 23 '10 at 17:22














                                              1












                                              1








                                              1







                                              Sure. Just generate a color using random RGB values. Like:



                                              public Color randomColor()
                                              {
                                              Random random=new Random(); // Probably really put this somewhere where it gets executed only once
                                              int red=random.nextInt(256);
                                              int green=random.nextInt(256);
                                              int blue=random.nextInt(256);
                                              return new Color(red, green, blue);
                                              }


                                              You might want to vary up the generation of the random numbers if you don't like the colors it comes up with. I'd guess these will tend to be fairly dark.






                                              share|improve this answer













                                              Sure. Just generate a color using random RGB values. Like:



                                              public Color randomColor()
                                              {
                                              Random random=new Random(); // Probably really put this somewhere where it gets executed only once
                                              int red=random.nextInt(256);
                                              int green=random.nextInt(256);
                                              int blue=random.nextInt(256);
                                              return new Color(red, green, blue);
                                              }


                                              You might want to vary up the generation of the random numbers if you don't like the colors it comes up with. I'd guess these will tend to be fairly dark.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 22 '10 at 14:30









                                              JayJay

                                              22.3k74787




                                              22.3k74787








                                              • 1





                                                It's great. but, what can i do for creating lighter color?

                                                – Elton.fd
                                                Nov 22 '10 at 14:44











                                              • you can use the Color.brighter() method to make any generated color look like.

                                                – Andrew
                                                Nov 22 '10 at 14:48






                                              • 1





                                                Sandra, to influence the brightness, make sure the random values are never very dark. 0 is darkest and 255 is brightest, so just do a random.nextInt(128) + 128 for example to never get any colers darker than half brightness.

                                                – Stijn de Witt
                                                Nov 22 '10 at 14:49











                                              • yes, It works nice, thanks :)

                                                – Elton.fd
                                                Nov 22 '10 at 15:03











                                              • @Stijn: Ditto. I might add that if you want more uniform brightness, you might make the 2nd value depend on the 1st and the 3rd depending on the first two. Like say red=nextInt(255); green=nextInt(255-red); etc. You could play with this sort of thing endlessly until you get the results you want.

                                                – Jay
                                                Nov 23 '10 at 17:22














                                              • 1





                                                It's great. but, what can i do for creating lighter color?

                                                – Elton.fd
                                                Nov 22 '10 at 14:44











                                              • you can use the Color.brighter() method to make any generated color look like.

                                                – Andrew
                                                Nov 22 '10 at 14:48






                                              • 1





                                                Sandra, to influence the brightness, make sure the random values are never very dark. 0 is darkest and 255 is brightest, so just do a random.nextInt(128) + 128 for example to never get any colers darker than half brightness.

                                                – Stijn de Witt
                                                Nov 22 '10 at 14:49











                                              • yes, It works nice, thanks :)

                                                – Elton.fd
                                                Nov 22 '10 at 15:03











                                              • @Stijn: Ditto. I might add that if you want more uniform brightness, you might make the 2nd value depend on the 1st and the 3rd depending on the first two. Like say red=nextInt(255); green=nextInt(255-red); etc. You could play with this sort of thing endlessly until you get the results you want.

                                                – Jay
                                                Nov 23 '10 at 17:22








                                              1




                                              1





                                              It's great. but, what can i do for creating lighter color?

                                              – Elton.fd
                                              Nov 22 '10 at 14:44





                                              It's great. but, what can i do for creating lighter color?

                                              – Elton.fd
                                              Nov 22 '10 at 14:44













                                              you can use the Color.brighter() method to make any generated color look like.

                                              – Andrew
                                              Nov 22 '10 at 14:48





                                              you can use the Color.brighter() method to make any generated color look like.

                                              – Andrew
                                              Nov 22 '10 at 14:48




                                              1




                                              1





                                              Sandra, to influence the brightness, make sure the random values are never very dark. 0 is darkest and 255 is brightest, so just do a random.nextInt(128) + 128 for example to never get any colers darker than half brightness.

                                              – Stijn de Witt
                                              Nov 22 '10 at 14:49





                                              Sandra, to influence the brightness, make sure the random values are never very dark. 0 is darkest and 255 is brightest, so just do a random.nextInt(128) + 128 for example to never get any colers darker than half brightness.

                                              – Stijn de Witt
                                              Nov 22 '10 at 14:49













                                              yes, It works nice, thanks :)

                                              – Elton.fd
                                              Nov 22 '10 at 15:03





                                              yes, It works nice, thanks :)

                                              – Elton.fd
                                              Nov 22 '10 at 15:03













                                              @Stijn: Ditto. I might add that if you want more uniform brightness, you might make the 2nd value depend on the 1st and the 3rd depending on the first two. Like say red=nextInt(255); green=nextInt(255-red); etc. You could play with this sort of thing endlessly until you get the results you want.

                                              – Jay
                                              Nov 23 '10 at 17:22





                                              @Stijn: Ditto. I might add that if you want more uniform brightness, you might make the 2nd value depend on the 1st and the 3rd depending on the first two. Like say red=nextInt(255); green=nextInt(255-red); etc. You could play with this sort of thing endlessly until you get the results you want.

                                              – Jay
                                              Nov 23 '10 at 17:22











                                              1














                                              You seem to want light random colors. Not sure what you mean exactly with light. But if you want random 'rainbow colors', try this



                                              Random r = new Random();
                                              Color c = Color.getHSBColor(r.nextFloat(),//random hue, color
                                              1.0,//full saturation, 1.0 for 'colorful' colors, 0.0 for grey
                                              1.0 //1.0 for bright, 0.0 for black
                                              );


                                              Search for HSB color model for more information.






                                              share|improve this answer




























                                                1














                                                You seem to want light random colors. Not sure what you mean exactly with light. But if you want random 'rainbow colors', try this



                                                Random r = new Random();
                                                Color c = Color.getHSBColor(r.nextFloat(),//random hue, color
                                                1.0,//full saturation, 1.0 for 'colorful' colors, 0.0 for grey
                                                1.0 //1.0 for bright, 0.0 for black
                                                );


                                                Search for HSB color model for more information.






                                                share|improve this answer


























                                                  1












                                                  1








                                                  1







                                                  You seem to want light random colors. Not sure what you mean exactly with light. But if you want random 'rainbow colors', try this



                                                  Random r = new Random();
                                                  Color c = Color.getHSBColor(r.nextFloat(),//random hue, color
                                                  1.0,//full saturation, 1.0 for 'colorful' colors, 0.0 for grey
                                                  1.0 //1.0 for bright, 0.0 for black
                                                  );


                                                  Search for HSB color model for more information.






                                                  share|improve this answer













                                                  You seem to want light random colors. Not sure what you mean exactly with light. But if you want random 'rainbow colors', try this



                                                  Random r = new Random();
                                                  Color c = Color.getHSBColor(r.nextFloat(),//random hue, color
                                                  1.0,//full saturation, 1.0 for 'colorful' colors, 0.0 for grey
                                                  1.0 //1.0 for bright, 0.0 for black
                                                  );


                                                  Search for HSB color model for more information.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 22 '10 at 15:37









                                                  IshtarIshtar

                                                  10.3k11830




                                                  10.3k11830























                                                      0














                                                      package com.adil.util;

                                                      /**
                                                      * The Class RandomColor.
                                                      *
                                                      * @author Adil OUIDAD
                                                      * @URL : http://kizana.fr
                                                      */
                                                      public class RandomColor {
                                                      /**
                                                      * Gets the random color.
                                                      *
                                                      * @return the random color
                                                      */
                                                      public static String getRandomColor() {
                                                      String letters = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
                                                      String color = "#";
                                                      for (int i = 0; i < 6; i++ ) {
                                                      color += letters[(int) Math.round(Math.random() * 15)];
                                                      }
                                                      return color;
                                                      }
                                                      }





                                                      share|improve this answer






























                                                        0














                                                        package com.adil.util;

                                                        /**
                                                        * The Class RandomColor.
                                                        *
                                                        * @author Adil OUIDAD
                                                        * @URL : http://kizana.fr
                                                        */
                                                        public class RandomColor {
                                                        /**
                                                        * Gets the random color.
                                                        *
                                                        * @return the random color
                                                        */
                                                        public static String getRandomColor() {
                                                        String letters = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
                                                        String color = "#";
                                                        for (int i = 0; i < 6; i++ ) {
                                                        color += letters[(int) Math.round(Math.random() * 15)];
                                                        }
                                                        return color;
                                                        }
                                                        }





                                                        share|improve this answer




























                                                          0












                                                          0








                                                          0







                                                          package com.adil.util;

                                                          /**
                                                          * The Class RandomColor.
                                                          *
                                                          * @author Adil OUIDAD
                                                          * @URL : http://kizana.fr
                                                          */
                                                          public class RandomColor {
                                                          /**
                                                          * Gets the random color.
                                                          *
                                                          * @return the random color
                                                          */
                                                          public static String getRandomColor() {
                                                          String letters = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
                                                          String color = "#";
                                                          for (int i = 0; i < 6; i++ ) {
                                                          color += letters[(int) Math.round(Math.random() * 15)];
                                                          }
                                                          return color;
                                                          }
                                                          }





                                                          share|improve this answer















                                                          package com.adil.util;

                                                          /**
                                                          * The Class RandomColor.
                                                          *
                                                          * @author Adil OUIDAD
                                                          * @URL : http://kizana.fr
                                                          */
                                                          public class RandomColor {
                                                          /**
                                                          * Gets the random color.
                                                          *
                                                          * @return the random color
                                                          */
                                                          public static String getRandomColor() {
                                                          String letters = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
                                                          String color = "#";
                                                          for (int i = 0; i < 6; i++ ) {
                                                          color += letters[(int) Math.round(Math.random() * 15)];
                                                          }
                                                          return color;
                                                          }
                                                          }






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Nov 17 '13 at 14:38









                                                          Hitham S. AlQadheeb

                                                          11.9k33262




                                                          11.9k33262










                                                          answered Jun 8 '13 at 20:54









                                                          user1942990user1942990

                                                          6416




                                                          6416























                                                              0














                                                              Here is a method for getting a random color:



                                                              private static Random sRandom;

                                                              public static synchronized int randomColor() {
                                                              if (sRandom == null) {
                                                              sRandom = new Random();
                                                              }
                                                              return 0xff000000 + 256 * 256 * sRandom.nextInt(256) + 256 * sRandom.nextInt(256)
                                                              + sRandom.nextInt(256);
                                                              }


                                                              Benefits:




                                                              • Get the integer representation which can be used with java.awt.Color or android.graphics.Color

                                                              • Keep a static reference to Random.






                                                              share|improve this answer






























                                                                0














                                                                Here is a method for getting a random color:



                                                                private static Random sRandom;

                                                                public static synchronized int randomColor() {
                                                                if (sRandom == null) {
                                                                sRandom = new Random();
                                                                }
                                                                return 0xff000000 + 256 * 256 * sRandom.nextInt(256) + 256 * sRandom.nextInt(256)
                                                                + sRandom.nextInt(256);
                                                                }


                                                                Benefits:




                                                                • Get the integer representation which can be used with java.awt.Color or android.graphics.Color

                                                                • Keep a static reference to Random.






                                                                share|improve this answer




























                                                                  0












                                                                  0








                                                                  0







                                                                  Here is a method for getting a random color:



                                                                  private static Random sRandom;

                                                                  public static synchronized int randomColor() {
                                                                  if (sRandom == null) {
                                                                  sRandom = new Random();
                                                                  }
                                                                  return 0xff000000 + 256 * 256 * sRandom.nextInt(256) + 256 * sRandom.nextInt(256)
                                                                  + sRandom.nextInt(256);
                                                                  }


                                                                  Benefits:




                                                                  • Get the integer representation which can be used with java.awt.Color or android.graphics.Color

                                                                  • Keep a static reference to Random.






                                                                  share|improve this answer















                                                                  Here is a method for getting a random color:



                                                                  private static Random sRandom;

                                                                  public static synchronized int randomColor() {
                                                                  if (sRandom == null) {
                                                                  sRandom = new Random();
                                                                  }
                                                                  return 0xff000000 + 256 * 256 * sRandom.nextInt(256) + 256 * sRandom.nextInt(256)
                                                                  + sRandom.nextInt(256);
                                                                  }


                                                                  Benefits:




                                                                  • Get the integer representation which can be used with java.awt.Color or android.graphics.Color

                                                                  • Keep a static reference to Random.







                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Apr 29 '16 at 22:55

























                                                                  answered Jan 19 '15 at 8:54









                                                                  Jared RummlerJared Rummler

                                                                  29.4k1396119




                                                                  29.4k1396119























                                                                      -1














                                                                      It would be helpful.



                                                                      Random randomGenerator = new Random();
                                                                      int red = randomGenerator.nextInt(255);
                                                                      int green = randomGenerator.nextInt(255);
                                                                      int blue = randomGenerator.nextInt(255);
                                                                      Color randomColour = new Color(red,green,blue);





                                                                      share|improve this answer


























                                                                      • All you did was copy-paste the other answer.

                                                                        – Boann
                                                                        Dec 3 '14 at 13:46











                                                                      • I tried this..It is working..that's whay I prefer this..

                                                                        – K.Mahesh
                                                                        Dec 4 '14 at 5:10











                                                                      • Oh I see. That's not what we do on Stack Overflow. The thing to do is earn 15 rep on the site (by posting new useful questions and answers), so you can vote up the answers you find useful.

                                                                        – Boann
                                                                        Dec 4 '14 at 13:01











                                                                      • Okay..Next time I'll focus on that.

                                                                        – K.Mahesh
                                                                        Dec 5 '14 at 5:45
















                                                                      -1














                                                                      It would be helpful.



                                                                      Random randomGenerator = new Random();
                                                                      int red = randomGenerator.nextInt(255);
                                                                      int green = randomGenerator.nextInt(255);
                                                                      int blue = randomGenerator.nextInt(255);
                                                                      Color randomColour = new Color(red,green,blue);





                                                                      share|improve this answer


























                                                                      • All you did was copy-paste the other answer.

                                                                        – Boann
                                                                        Dec 3 '14 at 13:46











                                                                      • I tried this..It is working..that's whay I prefer this..

                                                                        – K.Mahesh
                                                                        Dec 4 '14 at 5:10











                                                                      • Oh I see. That's not what we do on Stack Overflow. The thing to do is earn 15 rep on the site (by posting new useful questions and answers), so you can vote up the answers you find useful.

                                                                        – Boann
                                                                        Dec 4 '14 at 13:01











                                                                      • Okay..Next time I'll focus on that.

                                                                        – K.Mahesh
                                                                        Dec 5 '14 at 5:45














                                                                      -1












                                                                      -1








                                                                      -1







                                                                      It would be helpful.



                                                                      Random randomGenerator = new Random();
                                                                      int red = randomGenerator.nextInt(255);
                                                                      int green = randomGenerator.nextInt(255);
                                                                      int blue = randomGenerator.nextInt(255);
                                                                      Color randomColour = new Color(red,green,blue);





                                                                      share|improve this answer















                                                                      It would be helpful.



                                                                      Random randomGenerator = new Random();
                                                                      int red = randomGenerator.nextInt(255);
                                                                      int green = randomGenerator.nextInt(255);
                                                                      int blue = randomGenerator.nextInt(255);
                                                                      Color randomColour = new Color(red,green,blue);






                                                                      share|improve this answer














                                                                      share|improve this answer



                                                                      share|improve this answer








                                                                      edited Dec 3 '14 at 6:12









                                                                      Evan Knowles

                                                                      5,69112359




                                                                      5,69112359










                                                                      answered Dec 3 '14 at 5:27









                                                                      K.MaheshK.Mahesh

                                                                      11




                                                                      11













                                                                      • All you did was copy-paste the other answer.

                                                                        – Boann
                                                                        Dec 3 '14 at 13:46











                                                                      • I tried this..It is working..that's whay I prefer this..

                                                                        – K.Mahesh
                                                                        Dec 4 '14 at 5:10











                                                                      • Oh I see. That's not what we do on Stack Overflow. The thing to do is earn 15 rep on the site (by posting new useful questions and answers), so you can vote up the answers you find useful.

                                                                        – Boann
                                                                        Dec 4 '14 at 13:01











                                                                      • Okay..Next time I'll focus on that.

                                                                        – K.Mahesh
                                                                        Dec 5 '14 at 5:45



















                                                                      • All you did was copy-paste the other answer.

                                                                        – Boann
                                                                        Dec 3 '14 at 13:46











                                                                      • I tried this..It is working..that's whay I prefer this..

                                                                        – K.Mahesh
                                                                        Dec 4 '14 at 5:10











                                                                      • Oh I see. That's not what we do on Stack Overflow. The thing to do is earn 15 rep on the site (by posting new useful questions and answers), so you can vote up the answers you find useful.

                                                                        – Boann
                                                                        Dec 4 '14 at 13:01











                                                                      • Okay..Next time I'll focus on that.

                                                                        – K.Mahesh
                                                                        Dec 5 '14 at 5:45

















                                                                      All you did was copy-paste the other answer.

                                                                      – Boann
                                                                      Dec 3 '14 at 13:46





                                                                      All you did was copy-paste the other answer.

                                                                      – Boann
                                                                      Dec 3 '14 at 13:46













                                                                      I tried this..It is working..that's whay I prefer this..

                                                                      – K.Mahesh
                                                                      Dec 4 '14 at 5:10





                                                                      I tried this..It is working..that's whay I prefer this..

                                                                      – K.Mahesh
                                                                      Dec 4 '14 at 5:10













                                                                      Oh I see. That's not what we do on Stack Overflow. The thing to do is earn 15 rep on the site (by posting new useful questions and answers), so you can vote up the answers you find useful.

                                                                      – Boann
                                                                      Dec 4 '14 at 13:01





                                                                      Oh I see. That's not what we do on Stack Overflow. The thing to do is earn 15 rep on the site (by posting new useful questions and answers), so you can vote up the answers you find useful.

                                                                      – Boann
                                                                      Dec 4 '14 at 13:01













                                                                      Okay..Next time I'll focus on that.

                                                                      – K.Mahesh
                                                                      Dec 5 '14 at 5:45





                                                                      Okay..Next time I'll focus on that.

                                                                      – K.Mahesh
                                                                      Dec 5 '14 at 5:45


















                                                                      draft saved

                                                                      draft discarded




















































                                                                      Thanks for contributing an answer to Stack Overflow!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.




                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4246351%2fcreating-random-colour-in-java%23new-answer', 'question_page');
                                                                      }
                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      MongoDB - Not Authorized To Execute Command

                                                                      How to fix TextFormField cause rebuild widget in Flutter

                                                                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith