Processing
Storing and retrieving style settings

A style stack

Can be used in a similar way to the transformation matrix stack, but used to preserve the current processing rendering styles (e.g. fill colour, smooth status, font settings etc.).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Simple example to show how a style stack can be used to set temporary
// style values, then restore previous ones before continuing.

StyleStack ss;

void setup()
{
  size(400,400);
  ss = new StyleStack(); 
  
  // Print out current settings
  ss.pushStyle();
  ss.peekStyle();
  ss.popStyle();
}

void draw()
{
  background(255);
 
  // Store default styles.
  ss.pushStyle();
  
  // Customise styles for one object.
  smooth(); 
  noStroke();
  fill(255,255,100); 
  ellipse(width/2,height*.2,50,50);
  
  // Restore default styles.
  ss.popStyle();
  
  ellipse(width/2,height*.5,50,50);
}

StyleStack code

Create a new tab in your sketch called StyleStack and copy the code below into it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import java.util.Stack;

// *****************************************************************************
/** Class to allow style settings to be pushed/popped to and from a stack.
    @author Jo Wood
    @version 1.1, 28th August, 2008. */ 
// *****************************************************************************

public class StyleStack
{
  // ----------------------- Object Variables -----------------------
 
  //private ArrayDeque stack;  Use this instead if Java 6 is ever standard.
  private Stack stack;
 
  // -------------------------- Constructor -------------------------
 
  public StyleStack()
  {
    stack = new Stack(); 
  }
 
  // --------------------------- Methods ----------------------------
 
  /** Places the current style settings on the stack
    */
  public void pushStyle()
  {   
    Styles styles = new Styles();
    styles.colorMode       = g.colorMode;
    styles.colorModeX      = g.colorModeX;
    styles.colorModeY      = g.colorModeY;
    styles.colorModeZ      = g.colorModeZ;
    styles.colorModeA      = g.colorModeA;
    styles.ellipseMode     = g.ellipseMode;
    styles.imageMode       = g.imageMode;
    styles.rectMode        = g.rectMode;
    styles.textMode        = g.textMode;
    styles.textureMode     = g.textureMode;
   
    styles.strokeWeight    = g.strokeWeight;
    styles.strokeJoin      = g.strokeJoin;
    styles.strokeCap       = g.strokeCap;
   
    styles.isSmooth        = g.smooth;
    styles.isFill          = g.fill;
    styles.isStroke        = g.stroke;
    styles.isTint          = g.tint;
   
    styles.backgroundColor = g.backgroundColor;
    styles.fillColor       = g.fillColor;
    styles.strokeColor     = g.strokeColor;
    styles.tintColor       = g.tintColor;
    
    styles.textAlign       = g.textAlign;
    styles.textAlignY      = g.textAlignY;
    styles.textFont        = g.textFont;          // TODO: Clone object rather than copy reference.
    styles.textSize        = g.textSize;
    styles.textLeading     = g.textLeading;    
   
    stack.push(styles);
  }
 
  /** Restores the last style settings from the stack
    */
  public void popStyle()
  {
    if (stack.size() == 0)
    {
      System.out.println("Missing a pushStyle() to go with that popStyle(). Styles not changed.");     
    }
    else
    {
      Styles styles = (Styles)stack.pop();
      colorMode(styles.colorMode,styles.colorModeX,styles.colorModeY,styles.colorModeZ,styles.colorModeA);
      ellipseMode(styles.ellipseMode);
      imageMode(styles.imageMode);
      rectMode(styles.rectMode);
      textMode(styles.textMode);
      g.textureMode = styles.textureMode;
     
      strokeWeight(styles.strokeWeight);
      strokeJoin(styles.strokeJoin);
      strokeCap(styles.strokeCap);
       
      if (styles.isSmooth)
      {
        smooth();
      }
      else
      {
        noSmooth();
      }
     
      fill(styles.fillColor);
      
      if (!styles.isFill)
      {
        noFill();
      }
     
      stroke(styles.strokeColor);
      
      if (!styles.isStroke)
      {
        noStroke();
      }
     
      tint(styles.tintColor);
      if (!styles.isTint) 
      {
        noTint();
      }
   
      g.backgroundColor = styles.backgroundColor;
      textAlign(styles.textAlign);
      g.textAlignY = styles.textAlignY;
      g.textFont = styles.textFont;
      g.textSize = styles.textSize;
      textLeading(styles.textLeading);
    }
  }
  
  /** Prints out the last style settings from the stack
    */
  public void peekStyle()
  {
    if (stack.size() == 0)
    {
      System.out.println("Nothing on stack to view. Try adding a pushStyle() to place current settings onto stack.");     
    }
    else
    {
       println((Styles)stack.peek());
    }
  }
 
  // ------------------------ Nested Classes ------------------------
  
  private class Styles
  {  
    private int colorMode,ellipseMode,imageMode,rectMode,textMode,textureMode;
    private float colorModeA,colorModeX,colorModeY,colorModeZ;
    
    private float strokeWeight;
    private int strokeJoin,strokeCap;
    
    private boolean isSmooth,isFill,isStroke, isTint;
    
    private int backgroundColor,fillColor,strokeColor,tintColor;
    
    private int textAlign,textAlignY;
    private PFont textFont;
    private float textSize, textLeading;

  
    public String toString()
    {
      StringBuffer message = new StringBuffer("Current Processing styles:\n");
      message.append("colorMode: "+getColorModeText(colorMode)+" ["+colorModeX+","+colorModeY+","+colorModeZ+","+colorModeA+"]\n");
      message.append("ellipseMode: "+getShapeModeText(ellipseMode)+"\n");
      message.append("imageMode: "+getShapeModeText(imageMode)+"\n");
      message.append("rectMode: "+getShapeModeText(rectMode)+"\n");
      message.append("textMode: "+getTextModeText(textMode)+"\n");
      message.append("textureMode: "+getTextureModeText(textureMode)+"\n");
      
      if (isStroke) message.append("stroke is on\n"); else message.append("stroke is off\n");
      message.append("strokeColor: "+getColorText(strokeColor)+"\n");
      message.append("strokeWeight: "+strokeWeight+"\n");
      message.append("strokeJoin: "+getStrokeJoinText(strokeJoin)+"\n");
      message.append("strokeCap: "+getStrokeCapText(strokeCap)+"\n");
      
      if (isFill) message.append("fill is on\n"); else message.append("fill is off\n");
      message.append("fillColor: "+getColorText(fillColor)+"\n");
      
      if (isTint) message.append("tint is on\n"); else message.append("tint is off\n");
      message.append("tintColor: "+getColorText(tintColor)+"\n");
      
      if (isSmooth) message.append("smooth is on\n"); else message.append("smooth is off\n");
      message.append("backgroundColor: "+getColorText(backgroundColor)+"\n");
      
      if (textFont==null) message.append("no textFont defined\n"); else message.append("textFont is "+textFont.name+"\n");
      message.append("textSize: "+textSize+"\n");
      message.append("textAlign: "+textAlign+"\n");
      message.append("textAlignY: "+textAlignY+"\n");
      message.append("textLeading: "+textLeading+"\n");
      
      return message.toString();
    }
    
    
    private String getColorModeText(int colorMode)
    {
      switch (colorMode)
      {
        case RGB:
          return new String("RGB"); 
        case HSB:
          return new String("HSB");
        default:
          return new String("n/a ("+colorMode+")");
      }
    }
    
    
    private String getShapeModeText(int shapeMode)
    {
      switch (ellipseMode)
      {
        case CENTER:
          return new String("CENTER"); 
        case RADIUS:
          return new String("RADIUS");
        case CORNER:
          return new String("CORNER");
        case CORNERS:
          return new String("CORNERS");
        default:
          return new String("n/a ("+shapeMode+")");
      }
    }
    
    
    private String getTextModeText(int textMode)
    {
      switch (textMode)
      {
        case MODEL:
          return new String("MODEL"); 
        case SCREEN:
          return new String("SCREEN");
        case SHAPE:
          return new String("SHAPE");
        default:
          return new String("n/a ("+textMode+")");
      }
    }
    
    private String getTextureModeText(int textureMode)
    {
      switch (textureMode)
      {
        case IMAGE:
          return new String("IMAGE"); 
        case NORMALIZED:
          return new String("NORMALIZED");
        default:
          return new String("n/a ("+textureMode+")");
      }
    }
    
    private String getStrokeJoinText(int strokeJoin)
    {
      switch (strokeJoin)
      {
        case MITER:
          return new String("MITER"); 
        case BEVEL:
          return new String("BEVEL");
        case ROUND:
          return new String("ROUND");
        default:
          return new String("n/a ("+strokeJoin+")");
      }
    }
    
    private String getStrokeCapText(int strokeCap)
    {
      switch (strokeCap)
      {
        case SQUARE:
          return new String("SQUARE"); 
        case PROJECT:
          return new String("PROJECT");
        case ROUND:
          return new String("ROUND");
        default:
          return new String("n/a ("+strokeCap+")");
      }
    }
    
    private String getColorText(int colour)
    {
       Color jColour = new Color(colour,true);
       return new String("["+jColour.getRed()+","+jColour.getGreen()+","+jColour.getBlue()+","+jColour.getAlpha()+"]");
    }  
  }  
}
Last modified, 29th August, 2008