Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create Random color (Visual Basic)

Posted on 2004-04-22
7
Medium Priority
?
2,989 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 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Loops Section Overview

971 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