A method of achieving color transparent mixture with Java

zhaozj2021-02-08  282

Home: http://www.maxss.netemail: Maxss.net@163.com

When developing multimedia or game program, it is necessary to handle its transparency to produce better results. Here is a method for you how to implement a transparent mixture with a Java language. The RGB value of each point is mixed according to a certain ratio, which is determined by the alpha value, and the specific calculation is shown in:

Alpha = 0 ~ 100 r = (R1 * (100 - alpha) R2 * alpha) / 100 g = (G1 * (100 - alpha) g2 * alpha) / 100 b = (b1 * (100 - alpha) B2 * alpha) / 100 Example: RGB1 (232, 54, 13) RGB2 (94, 186, 233) alpha = 30 r = (232 * 70 94 * 30) / 100 = 190.6 -> 190 g = (54 * 70 186 * 30) / 100 = 93.6 -> 93 b = (13 * 70 233 * 30) / 100 = 79 alpha = 50 r = (232 * 50 94 * 50) / 100 = 163 g = (54 * 50 186 * 50) / 100 = 120 b = (13 * 50 233 * 50) / 100 = 123 This can be encapsulated into functions or classes in Java, so that the code is reused, corresponding Java code show as below:

Protected Color CalculateAlphavalue (Color C1, Color C2, Int Alpha)

{

IF (alpha

Alpha = min_alpha;

Else IF (alpha> max_alpha)

Alpha = MAX_ALPHA;

INT R = (C1.Getred () * (MAX_ALPHA - Alpha) C2.Getred () * alpha) / max_alpha;

INT g = (c1.getgreen () * (MAX_ALPHA - Alpha) C2.GETGREEN () * alpha) / max_alpha;

INT B = (c1.getBlue () * (MAX_ALPHA - Alpha) C2.GetBlue () * alpha) / max_alpha;

Return New Color (R, G, B);

}

转载请注明原文地址:https://www.9cbs.com/read-2160.html

New Post(0)