Source code: com/anotherbigidea/flash/structs/FillStyle.java
1 /****************************************************************
2 * Copyright (c) 2001, David N. Main, All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the
6 * following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * 3. The name of the author may not be used to endorse or
18 * promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ****************************************************************/
34 package com.anotherbigidea.flash.structs;
35
36 import java.io.*;
37 import com.anotherbigidea.io.OutStream;
38 import com.anotherbigidea.flash.SWFConstants;
39
40 public class FillStyle implements Style
41 {
42 protected int fillType;
43 protected Color color;
44 protected Matrix matrix;
45 protected int[] ratios; //for gradient fill
46 protected Color[] colors; //for gradient fill
47 protected int bitmapId;
48
49 public int getType() { return fillType; }
50 public Color getSolidColor() { return color; }
51 public Matrix getMatrix() { return matrix; }
52 public int getImageId() { return bitmapId; }
53 public int[] getGradientRatios() { return ratios; }
54 public Color[] getGradientColors() { return colors; }
55
56 /**
57 * Solid color fill (alpha depends on the TagDefineShapeX tag used)
58 */
59 public FillStyle( Color solidColor )
60 {
61 fillType = SWFConstants.FILL_SOLID;
62 color = solidColor;
63 }
64
65 /**
66 * Linear/Radial Gradient Fill
67 */
68 public FillStyle( Matrix matrix, int[] ratios,
69 Color[] colors, boolean radial )
70 {
71 this.matrix = matrix;
72 this.ratios = ratios;
73 this.colors = colors;
74
75 fillType = radial ? SWFConstants.FILL_RADIAL_GRADIENT :
76 SWFConstants.FILL_LINEAR_GRADIENT;
77 }
78
79 /**
80 * Bitmap fill
81 */
82 public FillStyle( int bitmapId, Matrix matrix, boolean clipped )
83 {
84 this.matrix = matrix;
85 this.bitmapId = bitmapId;
86
87 fillType = clipped ? SWFConstants.FILL_CLIPPED_BITMAP :
88 SWFConstants.FILL_TILED_BITMAP;
89 }
90
91 public void write( OutStream out, boolean hasAlpha ) throws IOException
92 {
93 out.writeUI8( fillType );
94
95 if( fillType == SWFConstants.FILL_SOLID )
96 {
97 if( hasAlpha ) color.writeWithAlpha( out );
98 else color.writeRGB( out );
99 }
100 else if( fillType == SWFConstants.FILL_LINEAR_GRADIENT
101 || fillType == SWFConstants.FILL_RADIAL_GRADIENT )
102 {
103 matrix.write( out );
104
105 int numRatios = ratios.length;
106
107 out.writeUI8( numRatios );
108
109 for( int i = 0; i < numRatios; i++ )
110 {
111 if( colors[i] == null ) continue;
112
113 out.writeUI8( ratios[i] );
114
115 if( hasAlpha ) colors[i].writeWithAlpha( out );
116 else colors[i].writeRGB( out );
117 }
118 }
119 else if( fillType == SWFConstants.FILL_TILED_BITMAP
120 || fillType == SWFConstants.FILL_CLIPPED_BITMAP )
121 {
122 out.writeUI16( bitmapId );
123 matrix.write( out );
124 }
125 }
126
127
128 public static void writeMorphFillStyle( OutStream out,
129 FillStyle startStyle,
130 FillStyle endStyle )
131 throws IOException
132 {
133 int fillType = startStyle.fillType;
134
135 out.writeUI8( fillType );
136
137 if( fillType == SWFConstants.FILL_SOLID )
138 {
139 startStyle.color.writeWithAlpha( out );
140 endStyle .color.writeWithAlpha( out );
141 }
142 else if( fillType == SWFConstants.FILL_LINEAR_GRADIENT
143 || fillType == SWFConstants.FILL_RADIAL_GRADIENT )
144 {
145 startStyle.matrix.write( out );
146 endStyle .matrix.write( out );
147
148 int numRatios = startStyle.ratios.length;
149 out.writeUI8( startStyle.ratios.length );
150
151 for( int i = 0; i < numRatios; i++ )
152 {
153 if( startStyle.colors[i] == null ||
154 endStyle.colors[i] == null ) continue;
155
156 out.writeUI8( startStyle.ratios[i] );
157 startStyle.colors[i].writeWithAlpha( out );
158
159 out.writeUI8( endStyle.ratios[i] );
160 endStyle.colors[i].writeWithAlpha( out );
161 }
162 }
163 else if( fillType == SWFConstants.FILL_TILED_BITMAP
164 || fillType == SWFConstants.FILL_CLIPPED_BITMAP )
165 {
166 int bitmapId = startStyle.bitmapId;
167
168 out.writeUI16( bitmapId );
169
170 startStyle.matrix.write( out );
171 endStyle .matrix.write( out );
172 }
173 }
174 }