Linear Gradient-linear-gradient and opacity/filter transparency compatibility solution and their rgb/rgba and hexadecimal conversion methods

Source: Internet
Author: User

Article 1: Introduction to the transparent effect compatibility solution of the filter opacity/filter RGB/rgba

The project needs to implement the transparent mask effect. The use of opacity/filter will make the internal elements transparent, and we do not want to make the actual text content transparent. At this time, we need to consider other implementation methods.

We know that RGB colors are commonly referred to as the three primary colors. The RGB color pattern is a color standard in the industry, through the red (R), Green (G), blue (B) the variation of the three color channels and their overlapping to obtain various colors are also called the coloring mode. The RGB mode is also called the RGB color space. The RGB color system includes almost all colors that human vision can perceive. It is one of the most widely used color systems.

It corresponds to the CMYK color mode suitable for printing, also known as the fading color mode. CMYK represents four colors for printing, c Represents cyan, m represents Magenta, Y represents yellow, and K represents Black ). In actual reference, it is difficult to combine cyan, magenta, and yellow to form a true black, but it is only brown at most. Therefore, K-Black is introduced. Black is used to strengthen the dark tone and deepen the dark color. CMYK mode is the best printing mode, because the RGB mode cannot be completely printed despite the many colors. Editing in CMYK mode can avoid color loss, but the operation speed is very slow. Therefore, the devices in reality, such as scanners and monitors, are both edited in RGB mode and then converted to CMYK mode. Some necessary color correction techniques are added to print/output.

In front-end development, we not only use RGB, but also rgba. In fact, rgba adds the Alpha transparency control parameter A based on RGB. The integer values of the R, G, and B parameters are 0 ~ 255, the value range of the percentage value is: 0.0% ~ 100.0%. A value out of the range will be used as of its nearest value limit. However, not all browsers support the percentage value. Parameter A, with a value ranging from 0 ~ Between 1, cannot be negative.

To put it bluntly, the rgba mode is a color setting method that contains transparent effects. The syntax is as follows:

//CSS Documentdiv {   background: rgba(0, 0, 0, 0.5);}

 

It allows us to add transparent colors to elements. Maybe we are used to "Opacity", which is easy to use. However, opacity will make all sub-elements transparent and it is difficult to solve this problem. (Unless you use a weird hack) cross-browser transparency is equally tricky.

With rgba, we can set an element to transparent without affecting its child elements:

Browser compatibility of rgba:

Type Internet Explorer Firefox Chrome Opera Safari
Version (×) IE6 (√) Firefox 3.0.10 (√) Chrome 2.0.x (√) Operators' 9.64 (√) Safari 4
(×) IE7        
(×) IE8        
(√) Ie9

Incompatible solution: declare a retain color

Not all browsers support rgba, so if allowed, you can declare a retained color. This color should be reliable-all browsers support it. If you do not declare it, no color is used in browsers that do not support rgba.

div {   background: rgb(200, 54, 54); /* The Fallback */   background: rgba(200, 54, 54, 0.5);}

 

However, this retreat is still ineffective in some antique browsers.

Support for rgba browsers

 

Browser, version, and operating system Test Results Return
Firefox 3.0.5 (OS X, Windows XP, Vista) Supported -
Firefox 2.0.0.18 (PC) Not Supported Solid color
Safari 4 (Developer Preview, Mac) Supported -
Safari 3.2.1 (PC) Supported -
Mobile Safari (iPhone) Supported -
Opera 9.6.1 Not Supported Solid color
Internet Explorer 5.5 (PC via ietester) Not Supported Colorless
IE 6 (PC via ietester) Not Supported Solid color
IE 7 Not Supported Solid color
IE 8 Beta 2 Not Supported Solid color
Google Chrome 1.0.154.43 Supported -
Google Chrome 1.0.154.46 Supported -
Netscape 4.8 (PC) Not Supported No color
SeaMonkey 1.1.14 Not Supported Colorless
SeaMonkey 1.1.16 Not Supported Solid color
SeaMonkey 2.0 beta3 Supported -
Sunrise 1.7.5 Supported -
Stainless 0.2.5 Supported -
Flock 2.0.2 Supported -
Blackberry storm Browser Supported Solid color
Camino 1.6.6 Not Supported Solid color

 

 

The above data is obtained through the test demo. The test page contains more complete browser compatibility lists. (Translated from: CSS-tricks)

Best Solution: Better exit for IE browser

Solution 1: css3pie

The css3pie plug-in allows IE 6-9 to output most of the very useful css3 modifier features. Of course, csspie allows color, background, and box-shadow to support rgba.

Div. demo {color: rgba (0, 0, 0 ,. 5); Background: rgba (0, 0 ,. 5);-pie-Background: rgba (0, 0 ,. 5);/* IE6-9 */box-Shadow: 1px 1px rgba (, 0 ,. 5);/* supports rgba only when fuzzy values are not set */}

 

Solution 2: Filter

Use the IE filter to make the background color transparent and simulate the rgba transparent effect.

Because ie supports conditional comments, we can discard RGB and use a private CSS Filter of IE to achieve the same effect:

<!--[if IE]>   <style type="text/css">   .color-block {       background:transparent;       filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#79C83636,endColorstr=#79C83636);       zoom: 1;    }    </style><![endif]-->

Note:Startcolorstr and endcolorstr values. The first two values are hexadecimal transparency, and the last six digits are hexadecimal colors.

2-bit transparency Conversion Method: X = Alpha * 255. Convert the calculated result X to hexadecimal format.

After testing, the rgba color can be used for border. However, different browsers do not support the rgba of border, but the only difference is that FF will overlap at the corner of border, for example, if the transparency is 0.4, the transparency of the four corners in FF will change to 0.8, but non-FF browsers that support rgba will not. I have not verified this.

Article 2: linear gradient-linear-gradient introduction, RGB/rgba and hexadecimal conversion methods


The following describes the linear gradient syntax, the rgba color mode, and the hexadecimal color transformation method based on the various network statements:

The color gradient attribute added by css3 greatly enriches the world of network colors, and further enhances the convenience of front-end developers and the maintainability of pages.

Syntax of linear gradient in Mozilla:

-Moz-linear-gradient ([<point >||< angle>,]? <Stop>, <Stop> [, <Stop>] *)

Syntax of linear gradient in WebKit:

-WebKit-linear-gradient ([<point >||< angle>,]? <Stop>, <Stop> [, <Stop>] *) // the latest release syntax.

-WebKit-gradient (<type>, <point> [, <radius>]?, <Point> [, <radius>]? [, <Stop>] *) // old-fashioned syntax writing

Syntax for linear gradient under Trident (IE:

Filter: progid: DXImageTransform. Microsoft. gradient (gradienttype = 0, startcolorstr = # 111ccc, endcolorstr = #1cc222 );

-MS-filter: "progid: DXImageTransform. Microsoft. gradient (gradienttype = 0, startcolorstr = # 111ccc, endcolorstr = #1cc222 )";

Comparison:

Style Background-image under Firefox:-moz-linear-gradient (rgba (255,255,255, 0.7) 0%, rgba (255,255,255, 0) 100% );

IE: filter: progid: DXImageTransform. Microsoft. gradient (startcolorstr = '# b2ffff', endcolorstr =' # 00ffffff', gradienttype = 0);/* IE6-9 */

Syntax:

Filter: progid: DXImageTransform. Microsoft. gradient (Enabled = benabled, startcolorstr = iwidth, endcolorstr = iwidth)

Attribute:

Enabled:Optional. Boolean ). Set or retrieve whether the filter is activated. True | false

True: default value. Filter activation.

False: the filter is disabled.

Startcolorstr:Optional. String ). Set or retrieve the start color and transparency of the color gradient.

The format is # 20.rggbb. AA, RR, GG, and BB are hexadecimal positive integers. The value range is 00-ff. Rr specifies the red value, GG specifies the green value, BB specifies the blue value, see # rrggbb color unit. AA specifies the transparency. 00 is completely transparent. FF is completely opaque. The value that exceeds the value range is restored to the default value.

The value range is # ff000000-# ffffff. The default value is # ff0000ff. Opaque blue.

Endcolorstr:Optional. String ). Set or retrieve the end color and transparency of the color gradient. See startcolorstr attributes. The default value is # ff000000. Opaque black.

Features:

Enabled: read/write. Boolean ). See enabled attributes.

Gradienttype: read/write. Integer ). Set or retrieve the direction of the color gradient. 1 | 0

1: default value. Horizontal gradient.

0: vertical gradient.

Startcolorstr: read/write. String ). See startcolorstr attributes.

Startcolor: read/write. Integer ). Set or retrieve the start color of the color gradient. Value Range: 0-4294967295. 0 is transparent. 4294967295 is not transparent white.

Endcolorstr: read/write. String ). Set or retrieve the end color and transparency of the color gradient. See startcolorstr attributes. The default value is # ff000000. Opaque black.

Endcolor: read/write. Integer ). Set or retrieve the ending color of the color gradient. Value Range: 0-4294967295. 0 is transparent. 4294967295 is not transparent white. When this feature is used in a script, it can also be in hexadecimal format: 0 x1_rggbb.

Note: custom color layers are displayed between the background and content of an object.

When this effect is displayed through a transformation, the procedural initialization of the text above the color layer of the gradient volume is transparent. After the color gradient is implemented, the text color is updated with the defined value.

Test DEMO code:

 

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

 

Pass the test in FF, chrome, and IE. Preview:

 

Appendix: practical cases of filters



Mourning day, all website pages are black and white, use the following filter !!

CSS Filter codes are provided to facilitate site mourning. The following is the full-site CSS code.

html { filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); }

Usage: This code changes the web page to black and white. You can add the code to the top of CSS to implement the installation.

If the website does not use CSS, you can insert the following between the HTML code of the webpage/template:

 

<style type="text/css">html{filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);}</style>

Some webmasters may not be able to use this CSS because the website does not use the latest standard webpage protocol.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Replace the header of the Web page with the above Code.

Some website Flash Animation colors cannot be controlled by CSS filters, and can be inserted between flash codes:

The simplest code for turning pages into gray is to add

<style>html {    FILTER: gray}</style>

 

Finally, some commonly used online color matching tools are attached:

Convert RGB color to hexadecimal colorHttp://www.quman.org/article/article0009.html

Gradient GeneratorHttp://gradcolor.com/

Css3 pie(Which makes Internet Explorer 6-9 capable of rendering several of the most useful css3 decoration features.) http://css3pie.com/

 

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.