]> git.sur5r.net Git - cc65/blob - src/sp65/palette.c
Merge remote-tracking branch 'upstream/master' into a5200
[cc65] / src / sp65 / palette.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 palette.c                                 */
4 /*                                                                           */
5 /*      Color palette definition for the sp65 sprite and bitmap utility      */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2012,      Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* common */
39 #include "check.h"
40 #include "xmalloc.h"
41
42 /* sp65 */
43 #include "palette.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Code                                    */
49 /*****************************************************************************/
50
51
52
53 Palette* NewPalette (unsigned Entries)
54 /* Create a new palette with the given number of entries */
55 {
56     Palette* P;
57
58     /* Some safety checks */
59     PRECONDITION (Entries > 0 && Entries <= 256);
60
61     /* Allocate memory */
62     P = xmalloc (sizeof (*P) + (Entries - 1) * sizeof (P->Entries[0]));
63
64     /* Initialize the struct, then return it */
65     P->Count = Entries;
66     return P;
67 }
68
69
70
71 Palette* NewMonochromePalette (void)
72 /* Create and return a palette with two entries (black and white) */
73 {
74     /* Create a new palette */
75     Palette* P = NewPalette (2);
76
77     /* Set the two colors */
78     P->Entries[0] = RGBA (0x00, 0x00, 0x00, 0x00);
79     P->Entries[1] = RGBA (0xFF, 0xFF, 0xFF, 0x00);
80
81     /* Return the new palette */
82     return P;
83 }
84
85
86
87 Palette* DupPalette (const Palette* P)
88 /* Create a copy of a palette */
89 {
90     Palette* N;
91
92
93     /* Allow to pass a NULL palette */
94     if (P == 0) {
95         return 0;
96     }
97
98     /* Create a new palette */
99     N = NewPalette (P->Count);
100
101     /* Copy the palette data */
102     memcpy (N->Entries, P->Entries, P->Count * sizeof (P->Entries[0]));
103
104     /* Return the copy */
105     return N;
106 }
107
108
109
110 void FreePalette (Palette* P)
111 /* Free a dynamically allocated palette */
112 {
113     xfree (P);
114 }
115
116
117
118 void DumpPalette (FILE* F, const Palette* P)
119 /* Dump the palette in readable form to the given file */
120 {
121     unsigned I;
122
123     fputs ("Entry     R      G      B      A     Combined\n", F);
124     fputs ("----------------------------------------------\n", F);
125     for (I = 0; I < P->Count; ++I) {
126
127         /* Get the color entry */
128         const Color* C = P->Entries + I;
129
130         /* Output it */
131         fprintf (F,
132                  " %3u    %3u    %3u    %3u    %3u    #%08lX\n",
133                  I,
134                  C->R, C->G, C->B, C->A,
135                  (((unsigned long) C->A) << 24) |
136                  (((unsigned long) C->B) << 16) |
137                  (((unsigned long) C->G) <<  8) |
138                  (((unsigned long) C->R) <<  0));
139     }
140 }