001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hslf.record;
019:
020: import org.apache.poi.util.LittleEndian;
021: import java.io.IOException;
022: import java.io.OutputStream;
023: import java.io.ByteArrayOutputStream;
024:
025: /**
026: * A ColorSchemeAtom (type 2032). Holds the 8 RGB values for the different
027: * colours of bits of text, that makes up a given colour scheme.
028: * Slides (presumably) link to a given colour scheme atom, and that
029: * defines the colours to be used
030: *
031: * @author Nick Burch
032: */
033:
034: public class ColorSchemeAtom extends RecordAtom {
035: private byte[] _header;
036: private static long _type = 2032l;
037:
038: private int backgroundColourRGB;
039: private int textAndLinesColourRGB;
040: private int shadowsColourRGB;
041: private int titleTextColourRGB;
042: private int fillsColourRGB;
043: private int accentColourRGB;
044: private int accentAndHyperlinkColourRGB;
045: private int accentAndFollowingHyperlinkColourRGB;
046:
047: /** Fetch the RGB value for Background Colour */
048: public int getBackgroundColourRGB() {
049: return backgroundColourRGB;
050: }
051:
052: /** Set the RGB value for Background Colour */
053: public void setBackgroundColourRGB(int rgb) {
054: backgroundColourRGB = rgb;
055: }
056:
057: /** Fetch the RGB value for Text And Lines Colour */
058: public int getTextAndLinesColourRGB() {
059: return textAndLinesColourRGB;
060: }
061:
062: /** Set the RGB value for Text And Lines Colour */
063: public void setTextAndLinesColourRGB(int rgb) {
064: textAndLinesColourRGB = rgb;
065: }
066:
067: /** Fetch the RGB value for Shadows Colour */
068: public int getShadowsColourRGB() {
069: return shadowsColourRGB;
070: }
071:
072: /** Set the RGB value for Shadows Colour */
073: public void setShadowsColourRGB(int rgb) {
074: shadowsColourRGB = rgb;
075: }
076:
077: /** Fetch the RGB value for Title Text Colour */
078: public int getTitleTextColourRGB() {
079: return titleTextColourRGB;
080: }
081:
082: /** Set the RGB value for Title Text Colour */
083: public void setTitleTextColourRGB(int rgb) {
084: titleTextColourRGB = rgb;
085: }
086:
087: /** Fetch the RGB value for Fills Colour */
088: public int getFillsColourRGB() {
089: return fillsColourRGB;
090: }
091:
092: /** Set the RGB value for Fills Colour */
093: public void setFillsColourRGB(int rgb) {
094: fillsColourRGB = rgb;
095: }
096:
097: /** Fetch the RGB value for Accent Colour */
098: public int getAccentColourRGB() {
099: return accentColourRGB;
100: }
101:
102: /** Set the RGB value for Accent Colour */
103: public void setAccentColourRGB(int rgb) {
104: accentColourRGB = rgb;
105: }
106:
107: /** Fetch the RGB value for Accent And Hyperlink Colour */
108: public int getAccentAndHyperlinkColourRGB() {
109: return accentAndHyperlinkColourRGB;
110: }
111:
112: /** Set the RGB value for Accent And Hyperlink Colour */
113: public void setAccentAndHyperlinkColourRGB(int rgb) {
114: accentAndHyperlinkColourRGB = rgb;
115: }
116:
117: /** Fetch the RGB value for Accent And Following Hyperlink Colour */
118: public int getAccentAndFollowingHyperlinkColourRGB() {
119: return accentAndFollowingHyperlinkColourRGB;
120: }
121:
122: /** Set the RGB value for Accent And Following Hyperlink Colour */
123: public void setAccentAndFollowingHyperlinkColourRGB(int rgb) {
124: accentAndFollowingHyperlinkColourRGB = rgb;
125: }
126:
127: /* *************** record code follows ********************** */
128:
129: /**
130: * For the Colour Scheme (ColorSchem) Atom
131: */
132: protected ColorSchemeAtom(byte[] source, int start, int len) {
133: // Sanity Checking - we're always 40 bytes long
134: if (len < 40) {
135: len = 40;
136: if (source.length - start < 40) {
137: throw new RuntimeException(
138: "Not enough data to form a ColorSchemeAtom (always 40 bytes long) - found "
139: + (source.length - start));
140: }
141: }
142:
143: // Get the header
144: _header = new byte[8];
145: System.arraycopy(source, start, _header, 0, 8);
146:
147: // Grab the rgb values
148: backgroundColourRGB = (int) LittleEndian.getInt(source,
149: start + 8 + 0);
150: textAndLinesColourRGB = (int) LittleEndian.getInt(source,
151: start + 8 + 4);
152: shadowsColourRGB = (int) LittleEndian.getInt(source,
153: start + 8 + 8);
154: titleTextColourRGB = (int) LittleEndian.getInt(source,
155: start + 8 + 12);
156: fillsColourRGB = (int) LittleEndian.getInt(source,
157: start + 8 + 16);
158: accentColourRGB = (int) LittleEndian.getInt(source,
159: start + 8 + 20);
160: accentAndHyperlinkColourRGB = (int) LittleEndian.getInt(source,
161: start + 8 + 24);
162: accentAndFollowingHyperlinkColourRGB = (int) LittleEndian
163: .getInt(source, start + 8 + 28);
164: }
165:
166: /**
167: * Create a new ColorSchemeAtom, to go with a new Slide
168: */
169: public ColorSchemeAtom() {
170: _header = new byte[8];
171: LittleEndian.putUShort(_header, 0, 16);
172: LittleEndian.putUShort(_header, 2, (int) _type);
173: LittleEndian.putInt(_header, 4, 32);
174:
175: // Setup the default rgb values
176: backgroundColourRGB = 16777215;
177: textAndLinesColourRGB = 0;
178: shadowsColourRGB = 8421504;
179: titleTextColourRGB = 0;
180: fillsColourRGB = 10079232;
181: accentColourRGB = 13382451;
182: accentAndHyperlinkColourRGB = 16764108;
183: accentAndFollowingHyperlinkColourRGB = 11711154;
184: }
185:
186: /**
187: * We are of type 3999
188: */
189: public long getRecordType() {
190: return _type;
191: }
192:
193: /**
194: * Convert from an integer RGB value to individual R, G, B 0-255 values
195: */
196: public static byte[] splitRGB(int rgb) {
197: byte[] ret = new byte[3];
198:
199: // Serialise to bytes, then grab the right ones out
200: ByteArrayOutputStream baos = new ByteArrayOutputStream();
201: try {
202: writeLittleEndian(rgb, baos);
203: } catch (IOException ie) {
204: // Should never happen
205: throw new RuntimeException(ie);
206: }
207: byte[] b = baos.toByteArray();
208: System.arraycopy(b, 0, ret, 0, 3);
209:
210: return ret;
211: }
212:
213: /**
214: * Convert from split R, G, B values to an integer RGB value
215: */
216: public static int joinRGB(byte r, byte g, byte b) {
217: return joinRGB(new byte[] { r, g, b });
218: }
219:
220: /**
221: * Convert from split R, G, B values to an integer RGB value
222: */
223: public static int joinRGB(byte[] rgb) {
224: if (rgb.length != 3) {
225: throw new RuntimeException(
226: "joinRGB accepts a byte array of 3 values, but got one of "
227: + rgb.length + " values!");
228: }
229: byte[] with_zero = new byte[4];
230: System.arraycopy(rgb, 0, with_zero, 0, 3);
231: with_zero[3] = 0;
232: int ret = (int) LittleEndian.getInt(with_zero, 0);
233: return ret;
234: }
235:
236: /**
237: * Write the contents of the record back, so it can be written
238: * to disk
239: */
240: public void writeOut(OutputStream out) throws IOException {
241: // Header - size or type unchanged
242: out.write(_header);
243:
244: // Write out the rgb values
245: writeLittleEndian(backgroundColourRGB, out);
246: writeLittleEndian(textAndLinesColourRGB, out);
247: writeLittleEndian(shadowsColourRGB, out);
248: writeLittleEndian(titleTextColourRGB, out);
249: writeLittleEndian(fillsColourRGB, out);
250: writeLittleEndian(accentColourRGB, out);
251: writeLittleEndian(accentAndHyperlinkColourRGB, out);
252: writeLittleEndian(accentAndFollowingHyperlinkColourRGB, out);
253: }
254:
255: /**
256: * Returns color by its index
257: *
258: * @param idx 0-based color index
259: * @return color by its index
260: */
261: public int getColor(int idx) {
262: int[] clr = { backgroundColourRGB, textAndLinesColourRGB,
263: shadowsColourRGB, titleTextColourRGB, fillsColourRGB,
264: accentColourRGB, accentAndHyperlinkColourRGB,
265: accentAndFollowingHyperlinkColourRGB };
266: return clr[idx];
267: }
268:
269: }
|