Вопрос

I have a code which contains colors in hex, and I would like to make this color randomly

 PetData = "1 24 #fff";

How could I make it?

Это было полезно?

Решение

Use String.Format and use the hex format for the arguments

var random = new Random();
var color = String.Format("#{0:X6}", random.Next(0x1000000));

this color variable gives you the hexadecimal code for random color.

If you want in RGB Format also refer this Link : Random Hex Color in C#

Другие советы

I would use a Guid.

A GUID is a 128-bit value consisting of one group of 8 hexadecimal digits, followed by three groups of 4 hexadecimal digits each, followed by one group of 12 hexadecimal digits.

Generate a new Guid() and parse out any six digits (excluding dashes):

public string GetRandomHexColor()
{
    var result = "#" + Guid.NewGuid().ToString().Substring(0, 6);
    return result;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top