# Painting

This chapter is from the book

### This chapter is from the book 

Gradients create smooth blends between two distinct colors. One of the most over-used graphic effects since the origination of desktop publishing, gradients have been extensively and poorly used in attempts to mimic the shadings evident in shadows and dimensions in the natural world.

Gradients do have some beneficial uses, however. When used to create subtle shading within a non-realistic element, gradients can produce desirable effects.

Gradients come in two flavors: linear and radial. Linear gradients transition colors across a straight path, whereas radial gradients transition colors between an outer circle and an inner circle (see Figure 7.5). To create a gradient, you need two items: an element that defines the gradient and a style rule that applies the gradient to the object.

Figure 7.5 Two types of gradients: linear (left) and radial (right).

In the case of the linear blend, the linearGradient element defines a gradient according to the direction of a line. This element contains attributes similar to the line element. x1 and y1 define the start point of the line, and x2 and y2 define the end point of the line. Thus, the element uses the following syntax: <linearGradient x1="A" x2="B" y1="C" y2="D">...</linearGradient>, where A, B, C, and D represent values defining the coordinates of the gradient's directional line (known as the "gradient vector").

Inside of the linearGradient element is a series of stop child elements. Each stop element contains two important components: an offset attribute and a stop-color style rule. The offset attribute determines the point on the gradient line at which a color is defined. This value is in relation to the start and stop points defined previously. Often, you will see the offset attribute listed as a percentage, although pixel values are also accepted. Thus, if a percentage is used, it is in relation to the length of the line defined in the linearGradient element. The stop-color style rule defines the color value at that stop element's point.

To create a gradient then, you will need at least two stop elements within your linearGradient element. The syntax for your stop elements will then appear as follows, where E and F represent points along the directional line (usually noted in percentages) and color-name defines a color:

```  <linearGradient x1="A" y1="B" x2="C" y2="D">
<stop offset="E" style="stop-color:color-name"/>
<stop offset="F" style="stop-color:color-name"/>

Just as with the stroke and fill declarations, the stop-color declaration can accept any color notation that SVG allows (including the common hexadecimal notation and color keywords).

To apply a gradient to an object, you'll need to add a gradient style rule. The style rule used for applying gradients is fill:url(#BlendID), where BlendID is the value of the gradient's id attribute.

The gradient can take advantage of the gradientUnits attribute to interpret the line data from the x1, y1 and x2, y2 coordinates. The attribute has two possible values: objectBoundingBox and userSpaceOnUse.

The objectBoundingBox value tells the gradient to base the coordinates on the object's bounding box. In other words, the (x1,y2) coordinates are calculated against the top left-most corner of the object, rather than the top left-most corner of the SVG document.

The userSpaceOnUse value tells the gradient to consider the line coordinates relative to the user coordinate system (generally the document's coordinate system, unless a transformation has been applied to the gradient beforehand). Thus, the (x1,y2) coordinates are usually calculated against the document's (0,0) point.

To demonstrate the linear gradient in action, you'll create a simple box with a white-to-black gradient fill, as shown in Listing 7.4:

1. First, create the gradient on line 14, naming it BlendLinear and drawing the gradient's path from 50,0 to 150,0 according to the SVG document's coordinate system.

2. Then, set two stops (lines 16 and 17): white at 0% (the 50,0 point) and black at 100% (the 150,0 point).

3. Lastly, create a class (line 8) that fills an object with the gradient, and then apply the class to your square (line 20).

#### Listing 7.4 Creating a Linear Gradient

```01: <?xml version="1.0" standalone="no"?>
02: <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
03:
04: <svg width="500" height="300">
05:
06: <style type="text/css">
07:   <![CDATA[
08:     .FillBlendLinear{fill:url(#BlendLinear);}
09:     .Stroke000000{stroke:#000000;}
10:     .StrokeWidth1{stroke-width:1;}
11:   ]]>
12: </style>
13:
15:   x1="50" y1="0" x2="150" y2="0">
16:     <stop offset="0%" style="stop-color:#FFFFFF"/>
17:     <stop offset="100%" style="stop-color:#000000"/>
19:
20: <rect class="FillBlendLinear StrokeWidth1 Stroke000000" x="50" y="50" width="100" height="100"/>
21:
22: </svg>```

Figure 7.6 shows the results of the added code.

Figure 7.6 Linear gradients can be used to create the blend apparent in this rectangle's fill.

Gradients can have multiple colors and stop points. You can add multiple stop elements to create a blend that spans several colors. Each stop element will then blend towards the color on either side of it. For instance, subtle metal effects are possible by blending across several light shades of gray.

To experiment with this possibility, you can alter your previous code (Listing 7.4) to include additional stop points, as in Listing 7.5. First, copy the two stop elements and paste between them, resulting in a total of four stop elements within your linearGradient element (lines 16 through 19 in Listing 7.5). Then, change the color values of each stop to a gray, and modify the two interior stop elements (lines 17 and 18) so that their offset value is somewhere between 0 and 100%. Figure 7.7 shows the result: a rectangle filled with various shades of gray, suggesting the appearance of a metal cylinder.

#### Listing 7.5 Adding Multiple Stops and Colors to a Gradient

```01: <?xml version="1.0" standalone="no"?>
02: <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
03:
04: <svg width="500" height="300">
05:
06: <style type="text/css">
07:   <![CDATA[
08:     .FillBlendLinear{fill:url(#BlendLinear);}
09:     .Stroke000000{stroke:#000000;}
10:     .StrokeWidth1{stroke-width:1;}
11:   ]]>
12: </style>
13:
15:   x1="50" y1="0" x2="150" y2="0">
16:     <stop offset="0%" style="stop-color:#666666"/>
17:     <stop offset="35%" style="stop-color:#999999"/>
18:     <stop offset="80%" style="stop-color:#333333"/>
19:     <stop offset="100%" style="stop-color:#666666"/>
21:
22: <rect class="FillBlendLinear StrokeWidth1 Stroke000000" x="50" y="50" width="100" height="100"/>
23:
24: </svg>```

Figure 7.7 Linear gradients with multiple stops can create multiple blends within one object.

Gradients need not go only from left to right; they can angle as well. By altering your line coordinates, you can change the direction of a gradient. Using Listing 7.5 as a starting point, you can alter your gradient by a 45º angle. Simply change the values in line 15 to reflect a line going from the lower left-hand corner of your square to the top right corner, as shown in Listing 7.6. Figure 7.8 shows the resulting angled gradient.

#### Listing 7.6 Changing the Gradient Vector Alters the Angle of the Gradient's Display

```01: <?xml version="1.0" standalone="no"?>
02: <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
03:
04: <svg width="500" height="300">
05:
06: <style type="text/css">
07:   <![CDATA[
08:     .FillBlendLinear{fill:url(#BlendLinear);}
09:     .Stroke000000{stroke:#000000;}
10:     .StrokeWidth1{stroke-width:1;}
11:   ]]>
12: </style>
13:
15:   x1="5" y1="95" x2="95" y2="5">
16:     <stop offset="0%" style="stop-color:#FF0000"/>
17:     <stop offset="35%" style="stop-color:#FFFFFF"/>
18:     <stop offset="80%" style="stop-color:#FFFFCC"/>
19:     <stop offset="100%" style="stop-color:#000000"/>
21:
22: <rect class="FillBlendLinear StrokeWidth1 Stroke000000" x="50" y="50" width="100" height="100"/>
23:
24: </svg>```

Figure 7.8 A linear gradient can be displayed on an angle by altering its directional line.

You can also leave off the line coordinates, at which point the gradient start point will be the left edge of the object, and the gradient will increase as it moves rightwards. To do so, you'll also need to remove the gradientUnits property. Thus, instead of <linearGradient x1="A" x2="B" y1="C" y2="D">...</linearGradient>, you can define your simplified gradient as <linearGradient>...</linearGradient>.

In the case of the radial blend, the radialGradient element contains attributes similar to the circle element. cx and cy define the circle, and r defines the radius of said circle. There are two additional properties that can be used to offset the circle's center: fx and fy. (As with the linearGradient element's directional attributes, all of these attributes can be left out.) The stop element functions just as it did for the linearGradient element.

To illustrate the radial blend, you'll create a glowing center for the news center graphic's sun in Listing 7.7. Using Listing 7.1 as a base, add a radialGradient element (line 15 in Listing 7.7). To determine how the cx and cy values will be interpreted, you will need to add a gradientUnits attribute.

In the last example, you used the value userSpaceOnUse, resulting in values that related to the document's coordinate system. In this case, use objectBoundingBox so that the other attribute's values are in relation to the coordinates of the object to which the gradient will be applied.

The cx and cy values (both defined as 50%) determine that the gradient will start in the middle of the applied object. The r value (also 50%) determines that the gradient will extend to the edges of its applied circle (as the radius of any circle is 50% of its actual dimensions). Two stop elements are created on lines 16 and 17, each defining a shade of yellow at their respective point along the gradient's radius (moving outward from the center point).

Lastly, the gradient is given an id value of GradientSunCenter (line 15). This value is then referenced when you create a style rule (on line 8) filling an object with that specific gradient. The rule is then applied to the circle on line 14, completing your document. Figure 7.9 shows the resulting image: a "glowing" sun.

#### Listing 7.7 Creating a Radial Gradient

```01: <?xml version="1.0" standalone="no"?>
02: <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
03:
04: <svg width="500" height="300">
05:
06:   <style type="text/css">
07:     <![CDATA[
09:       .Fill99CCFF{fill:#99CCFF;}
10:     ]]>
11:   </style>
12:
13:   <rect id="Sky" x="10" y="45" width="200" height="245" class="Fill99CCFF"/>
14:   <circle id="Sun" cx="105" cy="160" r="56" class="GradientSun"/>
16:     <stop offset="50%" style="stop-color:#FFFFCC"/>
17:     <stop offset="85%" style="stop-color:#FFFF00"/>
19:
20: </svg>```

Figure 7.9 A radial gradient can be used to create a glowing effect.

Lastly, note how even though the gradient elements existed within the SVG file, they did not display unless applied through the style command. You can store your gradient elements inside many elements, including svg and g elements, as well as definition and symbol elements (see Hour 8, "Symbols").

### InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

## Overview

Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

## Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

### Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

### Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

### Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

### Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

### Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

### Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

## Other Collection and Use of Information

### Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

### Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

### Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

### Do Not Track

This site currently does not respond to Do Not Track signals.

## Security

Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

## Children

This site is not directed to children under the age of 13.

## Marketing

Pearson may send or direct marketing communications to users, provided that

• Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
• Such marketing is consistent with applicable law and Pearson's legal obligations.
• Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
• Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

## Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

## Choice/Opt-out

Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

## Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

## Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

## Sharing and Disclosure

Pearson may disclose personal information, as follows:

• As required by law.
• With the consent of the individual (or their parent, if the individual is a minor)
• In response to a subpoena, court order or legal process, to the extent permitted or required by law
• To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
• In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
• To investigate or address actual or suspected fraud or other illegal activities
• To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
• To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
• To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.