Click here to Skip to main content
15,894,539 members
Home / Discussions / C#
   

C#

 
AnswerRe: GUI and database synchronization Pin
Eddy Vluggen14-Jul-13 2:53
professionalEddy Vluggen14-Jul-13 2:53 
GeneralRe: GUI and database synchronization Pin
leone14-Jul-13 22:06
leone14-Jul-13 22:06 
GeneralRe: GUI and database synchronization Pin
Eddy Vluggen15-Jul-13 0:30
professionalEddy Vluggen15-Jul-13 0:30 
AnswerRe: GUI and database synchronization Pin
Nicolás Marzoni16-Jul-13 9:44
Nicolás Marzoni16-Jul-13 9:44 
QuestionC# Using random Function Pin
Pintu Saw13-Jul-13 4:39
Pintu Saw13-Jul-13 4:39 
AnswerRe: C# Using random Function Pin
Dave Kreskowiak13-Jul-13 4:53
mveDave Kreskowiak13-Jul-13 4:53 
AnswerRe: C# Using random Function Pin
NotPolitcallyCorrect13-Jul-13 5:15
NotPolitcallyCorrect13-Jul-13 5:15 
AnswerRe: C# Using random Function Pin
harold aptroot13-Jul-13 5:52
harold aptroot13-Jul-13 5:52 
Generating a random colour is easy,
C#
Color.FromArgb(random.Next() | ~0x00FFFFFF)

~0x00FFFFFF is a trick to write unchecked((int)0xFF000000), and the bitwise OR with that value sets the Alpha component to 255 (ie, fully opaque).
It's the "most random" way to generate a colour, that is, each of the 16777216 opaque colours is generated with equal probability. Unfortunately, most of those colours look like poo.
The solution is to generate a random colour in HSV space,
C#
Color.FromArgb(HSV2RGB(random.NextDouble(), 1, 1))  
// 1, 1 makes saturated, bright colours

static int HSV2RGB(double h, double s, double v)
{
    int h_i = (int)(h * 6);
    double f = h * 6 - h_i;
    double p = v * (1 - s);
    double q = v * (1 - f * s);
    double t = v * (1 - (1 - f) * s);
    double r, g, b;
    switch (h_i)
    {
        default:
            r = v; g = t; b = p;
            break;
        case 1:
            r = q; g = v; b = p;
            break;
        case 2:
            r = p; g = v; b = t;
            break;
        case 3:
            r = p; g = q; b = v;
            break;
        case 4:
            r = t; g = p; b = v;
            break;
        case 5:
            r = v; g = p; b = q;
            break;
    }
    return (-0x01000000 | ((int)(255 * r) << 16)) | (((int)(255 * g) << 8) | (int)(255 * b));
}

-0x01000000 is yet an other trick to write unchecked((int)0xFF000000).

When generating multiple colours, this method still sort of fails: it will produce clashing colours. To avoid that, start with a random colour and add a specific offset
C#
double h_colour = random.NextDouble();
for (int i = 0; i < colours.Length; i++)
{
    colours[i] = Color.FromArgb(HSV2RGB(h_colour, 1, 1));
    h_colour = (h_colour + 0.618033988749895) % 1;  // that's the golden ratio
}


modified 15-Jul-13 14:28pm.

SuggestionRe: C# Using random Function Pin
Matt T Heffron15-Jul-13 8:21
professionalMatt T Heffron15-Jul-13 8:21 
GeneralRe: C# Using random Function Pin
harold aptroot15-Jul-13 8:29
harold aptroot15-Jul-13 8:29 
AnswerRe: C# Using random Function Pin
Kevin Bewley15-Jul-13 1:07
Kevin Bewley15-Jul-13 1:07 
QuestionRead Data from Access DB in ComboBox Pin
Hamed Parvini12-Jul-13 10:13
Hamed Parvini12-Jul-13 10:13 
AnswerRe: Read Data from Access DB in ComboBox Pin
Dave Kreskowiak12-Jul-13 15:05
mveDave Kreskowiak12-Jul-13 15:05 
QuestionNull array in a dictionary Pin
PozzaVecia11-Jul-13 20:42
PozzaVecia11-Jul-13 20:42 
QuestionRe: Null array in a dictionary Pin
Richard MacCutchan11-Jul-13 21:09
mveRichard MacCutchan11-Jul-13 21:09 
AnswerRe: Null array in a dictionary Pin
OriginalGriff11-Jul-13 21:10
mveOriginalGriff11-Jul-13 21:10 
GeneralRe: Null array in a dictionary Pin
PozzaVecia11-Jul-13 21:59
PozzaVecia11-Jul-13 21:59 
GeneralRe: Null array in a dictionary Pin
OriginalGriff11-Jul-13 22:24
mveOriginalGriff11-Jul-13 22:24 
GeneralRe: Null array in a dictionary Pin
PozzaVecia11-Jul-13 23:45
PozzaVecia11-Jul-13 23:45 
GeneralRe: Null array in a dictionary Pin
OriginalGriff11-Jul-13 23:58
mveOriginalGriff11-Jul-13 23:58 
GeneralRe: Null array in a dictionary Pin
Richard Deeming12-Jul-13 1:51
mveRichard Deeming12-Jul-13 1:51 
GeneralRe: Null array in a dictionary Pin
OriginalGriff12-Jul-13 3:37
mveOriginalGriff12-Jul-13 3:37 
GeneralRe: Null array in a dictionary Pin
Richard Deeming12-Jul-13 1:50
mveRichard Deeming12-Jul-13 1:50 
AnswerRe: Null array in a dictionary Pin
Jay Nardev12-Jul-13 1:11
Jay Nardev12-Jul-13 1:11 
QuestionRead Minidumps through Native Code Pin
Zach.Saunders11-Jul-13 6:02
Zach.Saunders11-Jul-13 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.