. Net c # generate a random color, which can control the brightness and generate a dark or bright color. The brightness of the color is determined based on the YUV mode,

Source: Internet
Author: User

. Net c # generate a random color, which can control the brightness and generate a dark or bright color. The brightness of the color is determined based on the YUV mode,

Random colors are often used in daily development. Sometimes color brightness must be controlled. For example, a random color on a webpage with a white background usually requires a slightly darker color, if the color is too bright, it may cause reading difficulties. For example, if you want to determine whether the background color is dark or bright, you can determine whether the foreground color is white or black.

The YUV color mode is reasonable and intuitive.

YUV is a color encoding method (PAL) used by European television systems. It is the color space used by the PAL and SECAM analog color TV systems. In Modern Color TV systems, three-channel color cameras or color CCD cameras are usually used to retrieve images, and then the acquired color image signals are divided by color, respectively amplified and corrected to obtain RGB, after the matrix conversion circuit, the Brightness Signal Y and two color difference signal B-Y (U) and R-Y (V) are obtained. Finally, the sending end encodes the brightness and color difference Signals respectively, send data over the same channel. The representation of this color is the so-called YUV color space representation. The importance of using YUV color space is that its brightness signal Y is separated from the color signal U and V.

 

The red part above explains the rationality and accuracy of this method. Specific information can be searched for YUV for further understanding. RGB can be converted into YUV values, we only need to use the Y value. Other values are ignored temporarily. The conversion formula is as follows:

Y = 0.299R + 0.587G + 0.114B

Two points: the value of 1 Y ranges from 0 to 255.

2. The greater the value of Y, the brighter the color.

Core code:

/// <Summary> /// generate color based on the custom range /// </summary> /// <param name = "start"> start value: 0-255 </ param> // <param name = "end"> end value: 0-255 </param> // <returns> Color </returns> private static Color MarkColor (int start, int end) {if (start <0 | start> 255) throw new Exception ("the starting value must be a number between 0 and "); if (end <0 | end> 255) throw new Exception ("the end value can only be a number between 0 and"); if (start> end) throw new Exception ("the start value cannot be greater than the end value" ); Random ran = new Random (Guid. newGuid (). getHashCode (); int R, G, B; double Y; bool result; do {R = ran. next (0,255); G = ran. next (1, 0,255); B = ran. next (0,255); // formula for calculating the value of Y = 0.299 * R + 0.587 * G + 0.114 * B; result = Y> = start & Y <= end ;} while (! Result); return Color. FromArgb (R, G, B );}

 

For ease of use, I encapsulated the method into a class library and provided the following static methods:

Public static Color MakeDarkColor (int borderline = 180); // obtain the dark Color. The default value is less than 180 public static Color MakeLightColor (int borderline = 180 ); // obtain the bright Color greater than 180 public static Color MakeAllColor () by default; // obtain all colors public static Color MakeColorByDefine (int start, int end ); // obtain the Color public static int GetY (color Color) according to the range; // obtain the color Y value

 

You only need to reference the namespace to use the above static methods of the MarkRandomColor class.

using ChengChenXu.com.MakeRandomColor;

 

Example:

Color color = MakeRandomColor.MarkLightColor();Color color = MakeRandomColor.MarkDarkColor(150);Color color = MakeRandomColor.MarkAllColor();

 

Finally, download the file:

Class Library: ChengChenXu.com.MakeRandomColor.rar

Source code and example: demo.rar

This article is original to the blogger. for reprinting, please keep the source:
Http://www.chengchenxu.com/Article/Show/16/yuv

 

Related Article

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.