#C14873. Color Blending Challenge
Color Blending Challenge
Color Blending Challenge
You are given a list of primary colors in RGB format and a list of blending ratio groups. Your task is to blend the primary colors according to each group of ratios. The blended color is computed by the formula:
\(R = \operatorname{round}(\sum_{i=1}^{n} ratio_i \times R_i)\)
\(G = \operatorname{round}(\sum_{i=1}^{n} ratio_i \times G_i)\)
\(B = \operatorname{round}(\sum_{i=1}^{n} ratio_i \times B_i)\)
where \(n\) is the number of primary colors and \(ratio_i\) is the normalized blending ratio for the i-th color. Before blending, each list of ratios must be normalized so that their sum is 1. If a list of ratios contains any negative values or sums to 0, it is considered invalid. Also, each primary color must have values between 0 and 255. Additionally, the number of ratios in each blending group must match the number of primary colors.
In case of any input violation, output the exact error message:
- "Empty input" if no primary colors or blending ratios are provided.
- "Invalid color value" if any primary color value is out of range [0,255].
- "Invalid blending ratio" if any ratio is negative or the sum of ratios equals 0.
- "Mismatch in number of primary colors and ratios" if the number of ratios provided does not match the number of primary colors.
The program should read from standard input and output to standard output. If an error occurs, simply output the error message.
inputFormat
The input is given from stdin in the following format:
n R1 G1 B1 R2 G2 B2 ... (n lines of primary colors) m ratio_11 ratio_12 ... ratio_1n ratio_21 ratio_22 ... ratio_2n ... (m lines of blending ratios)
Where:
n
is the number of primary colors. Ifn <= 0
then it is considered an empty input.- Each primary color is represented by three integers in the range [0,255].
m
is the number of blending queries.- Each blending query consists of exactly
n
floating point numbers representing the blending ratios.
outputFormat
The output is written to stdout. For each blending query, output one line with three integers separated by a space corresponding to the blended color in RGB format. If any error is encountered, output the corresponding error message.
## sample3
255 0 0
0 255 0
0 0 255
4
0.3333 0.3333 0.3333
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
85 85 85
255 0 0
0 255 0
0 0 255
</p>