4 * Copyright 1993,1994 Alexandre Julliard
7 static char Copyright
[] = "Copyright Alexandre Julliard, 1993,1994";
20 #define MAXINT INT_MAX
26 /* #define DEBUG_PALETTE /* */
27 /* #undef DEBUG_PALETTE /* */
30 extern void COLOR_SetMapping( DC
*dc
, HANDLE map
, WORD size
); /* color.c */
32 extern Colormap COLOR_WinColormap
;
35 /***********************************************************************
36 * CreatePalette (GDI.360)
38 HPALETTE
CreatePalette( LOGPALETTE
* palette
)
40 PALETTEOBJ
* palettePtr
;
44 size
= sizeof(LOGPALETTE
) + (palette
->palNumEntries
- 1) * sizeof(PALETTEENTRY
);
45 hpalette
= GDI_AllocObject( sizeof(GDIOBJHDR
) + size
, PALETTE_MAGIC
);
46 if (!hpalette
) return 0;
47 palettePtr
= (PALETTEOBJ
*) GDI_HEAP_ADDR( hpalette
);
48 memcpy( &palettePtr
->logpalette
, palette
, size
);
53 /***********************************************************************
54 * GetPaletteEntries (GDI.363)
56 WORD
GetPaletteEntries( HPALETTE hpalette
, WORD start
, WORD count
,
57 LPPALETTEENTRY entries
)
62 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
63 if (!palPtr
) return 0;
64 numEntries
= palPtr
->logpalette
.palNumEntries
;
65 if (start
>= numEntries
) return 0;
66 if (start
+count
> numEntries
) count
= numEntries
- start
;
67 memcpy( entries
, &palPtr
->logpalette
.palPalEntry
[start
],
68 count
* sizeof(PALETTEENTRY
) );
73 /***********************************************************************
74 * SetPaletteEntries (GDI.364)
76 WORD
SetPaletteEntries( HPALETTE hpalette
, WORD start
, WORD count
,
77 LPPALETTEENTRY entries
)
82 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
83 if (!palPtr
) return 0;
84 numEntries
= palPtr
->logpalette
.palNumEntries
;
85 if (start
>= numEntries
) return 0;
86 if (start
+count
> numEntries
) count
= numEntries
- start
;
87 memcpy( &palPtr
->logpalette
.palPalEntry
[start
], entries
,
88 count
* sizeof(PALETTEENTRY
) );
93 /***********************************************************************
94 * GetSystemPaletteEntries (GDI.375)
96 WORD
GetSystemPaletteEntries( HDC hdc
, WORD start
, WORD count
,
97 LPPALETTEENTRY entries
)
103 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
104 if (start
>= dc
->w
.devCaps
->sizePalette
) return 0;
105 if (start
+count
>= dc
->w
.devCaps
->sizePalette
)
106 count
= dc
->w
.devCaps
->sizePalette
- start
;
107 for (i
= 0; i
< count
; i
++)
109 color
.pixel
= start
+ i
;
110 XQueryColor( display
, COLOR_WinColormap
, &color
);
111 entries
[i
].peRed
= color
.red
>> 8;
112 entries
[i
].peGreen
= color
.green
>> 8;
113 entries
[i
].peBlue
= color
.blue
>> 8;
114 entries
[i
].peFlags
= 0;
120 /***********************************************************************
121 * GetNearestPaletteIndex (GDI.370)
123 WORD
GetNearestPaletteIndex( HPALETTE hpalette
, COLORREF color
)
125 int i
, minDist
, dist
;
128 PALETTEENTRY
* entry
;
131 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
132 if (!palPtr
) return 0;
134 if ((COLOR_WinColormap
!= DefaultColormapOfScreen(screen
)) &&
135 (hpalette
== STOCK_DEFAULT_PALETTE
))
137 if ((color
& 0xffffff) == 0) return 0; /* Entry 0 is black */
138 if ((color
& 0xffffff) == 0xffffff) /* Max entry is white */
139 return palPtr
->logpalette
.palNumEntries
- 1;
142 r
= GetRValue(color
);
143 g
= GetGValue(color
);
144 b
= GetBValue(color
);
146 entry
= palPtr
->logpalette
.palPalEntry
;
147 for (i
= 0, minDist
= MAXINT
; minDist
!=0 &&
148 i
< palPtr
->logpalette
.palNumEntries
; i
++)
150 if (entry
->peFlags
!= 0xff)
152 dist
= (r
- entry
->peRed
) * (r
- entry
->peRed
) +
153 (g
- entry
->peGreen
) * (g
- entry
->peGreen
) +
154 (b
- entry
->peBlue
) * (b
- entry
->peBlue
);
163 dprintf_palette(stddeb
,"GetNearestPaletteIndex(%x,%06x) : returning %d\n",
164 hpalette
, color
, index
);
169 /***********************************************************************
172 int PALETTE_GetObject( PALETTEOBJ
* palette
, int count
, LPSTR buffer
)
174 if (count
> sizeof(WORD
)) count
= sizeof(WORD
);
175 memcpy( buffer
, &palette
->logpalette
.palNumEntries
, count
);
180 /***********************************************************************
181 * GDISelectPalette (GDI.361)
183 HPALETTE
GDISelectPalette( HDC hdc
, HPALETTE hpal
)
188 dprintf_palette(stddeb
, "GDISelectPalette: %d %d\n", hdc
, hpal
);
189 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
190 prev
= dc
->w
.hPalette
;
191 dc
->w
.hPalette
= hpal
;
192 if (hpal
!= STOCK_DEFAULT_PALETTE
) COLOR_SetMapping( dc
, 0, 0 );
193 else RealizeDefaultPalette( hdc
); /* Always realize default palette */
198 /***********************************************************************
199 * GDIRealizePalette (GDI.362)
201 UINT
GDIRealizePalette( HDC hdc
)
203 dprintf_palette(stdnimp
, "GDIRealizePalette: %d\n", hdc
);
208 /***********************************************************************
209 * SelectPalette (USER.282)
211 HPALETTE
SelectPalette(HDC hDC
, HPALETTE hPal
, BOOL bForceBackground
)
213 return GDISelectPalette( hDC
, hPal
);
217 /***********************************************************************
218 * RealizePalette (USER.283)
220 UINT
RealizePalette(HDC hDC
)
222 return GDIRealizePalette( hDC
);