Solved

Create Random color (Visual Basic)

Posted on 2004-04-22
7
2,976 Views
Last Modified: 2013-12-04
how to create random color using RGB function without repeat of the same color or color code.

0
Comment
Question by:cyseng10
  • 3
7 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 10886974
Hi cyseng10,

Firstly I wouldn't use the RGB(R,G,B) function. Use a long instead. Which is actually what is returned by this function.

Something simple like:

Randomize
Color = Rnd() * &HFFFFFF

Will get you a random color, set the seed based on date & time for more randomness.

Then you need to store the returned long in a searchable list so that you can check whether it has been used before. This could be anything that makes sense, a disconnected recordset or an array or collection or whatever you like really as long as it can be searched for a matching value fairly quickly and easily.

Tim Cottee
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
LVL 1

Author Comment

by:cyseng10
ID: 10887258
thank you for suggestion. i will try on it ..

how i check if the color is black or dark? i will not useblack or too dark color. the color will apply into flexgrid.


0
 
LVL 43

Expert Comment

by:TimCottee
ID: 10887280
cyseng10,

That gets quite complicated. There is a question somewhere on here that addresses this, will see if I can find it.

Tim.
0
 
LVL 2

Expert Comment

by:Ivan_Skrinjaric
ID: 10887323
Yuo can control color brigtnes with brightnes variable.

    Dim color   As String
    Dim brightnes As Integer
    Dim invbrightnes As Integer

    brightnes = 200
    invbrightnes = 255 - brightnes

    color = RGB(brightnes + (Rnd * invbrightnes ), brightnes + (Rnd * invbrightnes ), brightnes + (Rnd * invbrightnes ))

Ivan.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 10946859
cyseng10,

Why the C grade? I haven't yet been able to locate the question I referred to. And now it is extremely unlikely that I will waste my time doing so. The answer given to the original (not subsequent) question was perfectly reasonable. If not then you should have explained why and allowed me to improve the answer. Please consider requesting a grade change by posting a question in CS to that effect.

Tim.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 94
Rubik's Cube Code for Effective Presentation 3 64
C# application error "Parameter Is Not Valid". 3 117
Eclipse Java import and method not resolved 4 52
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question