Deep colors are randomly generated in. Net (C #).

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.