Program Description
Colorgrads color gradient]
The colorgrads program uses startcolor and endcolor to generate a color gradient set.
Colors can be expressed in red (R), Green (G), and blue (B.
The program first converts the general color representation into a set of elements using red (R), Green (G), and blue (B) color values.
First, you need to know the color representation. The W3C colors section shows the following formats:
Keyword mode:
Em {color: Red}
RGB color mode:
Em {color: # f00}
Em {color: # ff0000}
Em {color: RGB (255, 0, 0 )}
Em {color: RGB (100%, 0%, 0% )}
The above indicates the same color (red ).
The format of obtaining the color attribute is different from that of FF. FF returns the third form of RGB color mode, and IE returns the result according to the set format.
First, let's talk about the RGB color mode. The first two are usually used to understand their differences. They are represented in hexadecimal notation, and we want 10 hexadecimal notation.
Convert a hexadecimal character to a decimal number. Generally, parseint is used. After the substr truncation, parseint can be used.
The # ff0000 format can be converted as follows:
CopyCode The Code is as follows: Return map ([color. substr (1, 2), color. substr (3, 2), color. substr (5, 2)],
Function (x) {return parseint (x, 16 );}
)
The second parameter of parseint is the hexadecimal value of the first parameter.
For the # f00 format, the last one is similar, but the complete representation must be replaced before conversion:Copy codeThe Code is as follows: Return map ([color. substr (1, 1), color. substr (2, 1), color. substr (3, 1)],
Function (x) {return parseint (x + X, 16 );}
)
The latter two may be less used. One is represented by a 10-digit RGB color value, and the other is represented by a percentage.
FF is expressed strictly in that format, while IE is "relaxed", for example:
Ie can allow mixed numeric percentages, and FF cannot;
FF must be separated by commas (,). ie can be separated by spaces;
Of course, we should set it according to W3C standards.
PS: The EM {color: RGB 1.0 0.0 0.0} written in the DHTML manual is useless and should not be misled.
In this form, the program uses a regular expression to obtain a value. If there is %, the corresponding value is calculated based on the percentage:Copy codeThe Code is as follows: Return map (color. Match (/\ D + (\. \ D + )? \ %? /G ),
Function (x) {return parseint (X. indexof ("%")> 0? Parsefloat (x, 10) * 2.55: X, 10 );}
)
The keywords are familiar to everyone, but it is troublesome to convert them, because there are no certain rules and they can only correspond one by one:Copy codeCode: var mapping = {"red": "# ff0000"}; // omitted
Color = mapping [color. tolowercase ()];
If (color ){
Return map ([color. substr (1, 2), color. substr (3, 2), color. substr (5, 2)],
Function (x) {return parseint (x, 16 );}
)
}
After you obtain the start color and end color data in the create color set program, you can obtain the Step Size Based on the step:Copy codeThe Code is as follows: startcolor = This. getcolor (this. startcolor ),
Endcolor = This. getcolor (this. endcolor ),
Stepr = (endcolor [0]-startcolor [0])/This. Step,
Stepg = (endcolor [1]-startcolor [1])/This. Step,
Stepb = (endcolor [2]-startcolor [2])/This. step;
Generate a set based on the step size:Copy codeThe Code is as follows: for (VAR I = 0, n = This. step, r = startcolor [0], G = startcolor [1], B = startcolor [2]; I <n; I ++ ){
Colors. Push ([R, G, B]); R + = stepr; G + = stepg; B + = stepb;
}
Colors. Push (endcolor );
The correct color value ranges from 0 to 255 without decimal digits, so it is best to correct it:Copy codeThe Code is as follows: Return map (colors, function (x) {return map (x, function (x ){
Return math. Min (math. Max (0, math. Floor (x), 255 );
});});