Sometimes we want to generate random deep colors to meet the actual needs, such as: Color QR code, in order to make the mobile phone and QR code identification devices can correctly identify, must use a deeper color.
As shown in:
So how to implement it?
The source code is as follows:
// C # code:
Using system;
Using system. drawing;
Namespace lgms. net. qrcode. codec. util
{
Public class colorutil
{
Public static system. Drawing. Color getrandomcolor ()
{
Random randomnum_1 = new random (guid. newguid (). gethashcode ());
System. Threading. thread. Sleep (randomnum_1.next (1 ));
Int int_red = randomnum_1.next (255 );
Random randomnum_2 = new random (INT) datetime. Now. ticks );
Int int_green = randomnum_2.next (255 );
Random randomnum_3 = new random (guid. newguid (). gethashcode ());
Int int_blue = randomnum_3.next (255 );
Int_blue = (int_red + int_green> 380 )? Int_red + int_green-380: int_blue;
Int_blue = (int_blue & gt; 255 )? 255: int_blue;
Return getdarkercolor (system. Drawing. color. fromargb (int_red, int_green, int_blue ));
}
// Obtain the deepening color
Public static color getdarkercolor (color)
{
Const int max = 255;
Int increase = new random (guid. newguid (). gethashcode (). Next (30,255); // you can adjust the value as needed.
Int r = math. Abs (math. Min (color. R-increase, max ));
Int G = math. Abs (math. Min (color. G-increase, max ));
Int B = math. Abs (math. Min (color. B-increase, max ));
Return color. fromargb (R, G, B );
}
}
Hope to be useful to friends in need.