2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: color.c,v 1.39 2004/05/10 17:07:20 weiden Exp $ */
22 // FIXME: Use PXLATEOBJ logicalToSystem instead of int *mapping
24 int COLOR_gapStart
= 256;
25 int COLOR_gapEnd
= -1;
26 int COLOR_gapFilled
= 0;
30 static HPALETTE hPrimaryPalette
= 0; // used for WM_PALETTECHANGED
32 //static HPALETTE hLastRealizedPalette = 0; // UnrealizeObject() needs it
34 const PALETTEENTRY COLOR_sysPalTemplate
[NB_RESERVED_COLORS
] =
36 // first 10 entries in the system palette
37 // red green blue flags
38 { 0x00, 0x00, 0x00, PC_SYS_USED
},
39 { 0x80, 0x00, 0x00, PC_SYS_USED
},
40 { 0x00, 0x80, 0x00, PC_SYS_USED
},
41 { 0x80, 0x80, 0x00, PC_SYS_USED
},
42 { 0x00, 0x00, 0x80, PC_SYS_USED
},
43 { 0x80, 0x00, 0x80, PC_SYS_USED
},
44 { 0x00, 0x80, 0x80, PC_SYS_USED
},
45 { 0xc0, 0xc0, 0xc0, PC_SYS_USED
},
46 { 0xc0, 0xdc, 0xc0, PC_SYS_USED
},
47 { 0xa6, 0xca, 0xf0, PC_SYS_USED
},
49 // ... c_min/2 dynamic colorcells
50 // ... gap (for sparse palettes)
51 // ... c_min/2 dynamic colorcells
53 { 0xff, 0xfb, 0xf0, PC_SYS_USED
},
54 { 0xa0, 0xa0, 0xa4, PC_SYS_USED
},
55 { 0x80, 0x80, 0x80, PC_SYS_USED
},
56 { 0xff, 0x00, 0x00, PC_SYS_USED
},
57 { 0x00, 0xff, 0x00, PC_SYS_USED
},
58 { 0xff, 0xff, 0x00, PC_SYS_USED
},
59 { 0x00, 0x00, 0xff, PC_SYS_USED
},
60 { 0xff, 0x00, 0xff, PC_SYS_USED
},
61 { 0x00, 0xff, 0xff, PC_SYS_USED
},
62 { 0xff, 0xff, 0xff, PC_SYS_USED
} // last 10
65 const PALETTEENTRY
* FASTCALL
COLOR_GetSystemPaletteTemplate(void)
67 return (const PALETTEENTRY
*)&COLOR_sysPalTemplate
;
70 BOOL STDCALL
NtGdiAnimatePalette(HPALETTE hPalette
, UINT uStartIndex
,
71 UINT uEntries
, CONST PPALETTEENTRY ppe
)
74 SetLastWin32Error(ERROR_CALL_NOT_IMPLEMENTED
);
78 HPALETTE STDCALL
NtGdiCreateHalftonePalette(HDC hDC
)
84 PALETTEENTRY aEntries
[256];
87 Palette
.Version
= 0x300;
88 Palette
.NumberOfEntries
= 256;
89 if (NtGdiGetSystemPaletteEntries(hDC
, 0, 256, Palette
.aEntries
) == 0)
94 for (r
= 0; r
< 6; r
++)
95 for (g
= 0; g
< 6; g
++)
96 for (b
= 0; b
< 6; b
++)
98 i
= r
+ g
*6 + b
*36 + 10;
99 Palette
.aEntries
[i
].peRed
= r
* 51;
100 Palette
.aEntries
[i
].peGreen
= g
* 51;
101 Palette
.aEntries
[i
].peBlue
= b
* 51;
104 for (i
= 216; i
< 246; i
++)
106 int v
= (i
- 216) << 3;
107 Palette
.aEntries
[i
].peRed
= v
;
108 Palette
.aEntries
[i
].peGreen
= v
;
109 Palette
.aEntries
[i
].peBlue
= v
;
112 return NtGdiCreatePalette((LOGPALETTE
*)&Palette
);
115 HPALETTE STDCALL
NtGdiCreatePalette(CONST PLOGPALETTE palette
)
119 HPALETTE NewPalette
= PALETTE_AllocPalette(
121 palette
->palNumEntries
,
122 (PULONG
)palette
->palPalEntry
,
125 PalGDI
= (PPALGDI
) PALETTE_LockPalette(NewPalette
);
127 PALETTE_ValidateFlags(PalGDI
->IndexedColors
, PalGDI
->NumColors
);
128 PalGDI
->logicalToSystem
= NULL
;
130 PALETTE_UnlockPalette(NewPalette
);
135 BOOL STDCALL
NtGdiGetColorAdjustment(HDC hDC
,
136 LPCOLORADJUSTMENT ca
)
141 COLORREF STDCALL
NtGdiGetNearestColor(HDC hDC
,
144 COLORREF nearest
= CLR_INVALID
;
151 HPALETTE hpal
= dc
->w
.hPalette
;
152 palGDI
= (PPALGDI
) PALETTE_LockPalette(hpal
);
159 nearest
= COLOR_LookupNearestColor(palGDI
->IndexedColors
,
160 palGDI
->NumColors
, Color
);
161 PALETTE_UnlockPalette(hpal
);
168 UINT STDCALL
NtGdiGetNearestPaletteIndex(HPALETTE hpal
,
171 PPALGDI palGDI
= (PPALGDI
) PALETTE_LockPalette(hpal
);
176 /* Return closest match for the given RGB color */
177 index
= COLOR_PaletteLookupPixel((LPPALETTEENTRY
)palGDI
->IndexedColors
, palGDI
->NumColors
, NULL
, Color
, FALSE
);
178 PALETTE_UnlockPalette(hpal
);
184 UINT STDCALL
NtGdiGetPaletteEntries(HPALETTE hpal
,
192 palGDI
= (PPALGDI
) PALETTE_LockPalette(hpal
);
198 numEntries
= palGDI
->NumColors
;
199 if (numEntries
< StartIndex
+ Entries
)
201 Entries
= numEntries
- StartIndex
;
205 if (numEntries
<= StartIndex
)
207 PALETTE_UnlockPalette(hpal
);
210 memcpy(pe
, &palGDI
->IndexedColors
[StartIndex
], Entries
* sizeof(PALETTEENTRY
));
211 for (numEntries
= 0; numEntries
< Entries
; numEntries
++)
213 if (pe
[numEntries
].peFlags
& 0xF0)
215 pe
[numEntries
].peFlags
= 0;
220 PALETTE_UnlockPalette(hpal
);
224 UINT STDCALL
NtGdiGetSystemPaletteEntries(HDC hDC
,
232 if (!(dc = AccessUserObject(hdc))) return 0;
236 Entries = dc->GDIInfo->ulNumPalReg;
240 if (StartIndex >= dc->GDIInfo->ulNumPalReg)
246 if (StartIndex + Entries >= dc->GDIInfo->ulNumPalReg) Entries = dc->GDIInfo->ulNumPalReg - StartIndex;
248 for (i = 0; i < Entries; i++)
250 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry(StartIndex + i);
254 // GDI_ReleaseObj(hdc);
256 // FIXME UNIMPLEMENTED;
260 UINT STDCALL
NtGdiGetSystemPaletteUse(HDC hDC
)
267 The RealizePalette function modifies the palette for the device associated with the specified device context. If the device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device context is a display DC, the physical palette for that device is modified.
269 A logical palette is a buffer between color-intensive applications and the system, allowing these applications to use as many colors as needed without interfering with colors displayed by other windows.
271 1= IF DRAWING TO A DEVICE
272 -- If it is a paletted bitmap, and is not an identity palette, then an XLATEOBJ is created between the logical palette and
274 -- If it is an RGB palette, then an XLATEOBJ is created between the RGB values and the system palette.
276 2= IF DRAWING TO A MEMORY DC\BITMAP
277 -- If it is a paletted bitmap, and is not an identity palette, then an XLATEOBJ is created between the logical palette and
279 -- If it is an RGB palette, then an XLATEOBJ is created between the RGB values and the dc palette.
281 UINT STDCALL
NtGdiRealizePalette(HDC hDC
)
283 PALOBJ
*palPtr
, *sysPtr
;
284 PPALGDI palGDI
, sysGDI
;
287 HPALETTE systemPalette
;
290 USHORT sysMode
, palMode
;
296 SurfGDI
= (PSURFGDI
)AccessInternalObject((ULONG
)dc
->Surface
);
297 systemPalette
= NtGdiGetStockObject((INT
)DEFAULT_PALETTE
);
298 palGDI
= PALETTE_LockPalette(dc
->w
.hPalette
);
299 palPtr
= (PALOBJ
*) palGDI
;
301 // Step 1: Create mapping of system palette\DC palette
303 realized
= PALETTE_SetMapping(palPtr
, 0, palGDI
->NumColors
,
304 (dc
->w
.hPalette
!= hPrimaryPalette
) ||
305 (dc
->w
.hPalette
== NtGdiGetStockObject(DEFAULT_PALETTE
)));
310 sysGDI
= PALETTE_LockPalette(systemPalette
);
311 sysPtr
= (PALOBJ
*) sysGDI
;
314 // The RealizePalette function modifies the palette for the device associated with the specified device context. If the
315 // device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device
316 // context is a display DC, the physical palette for that device is modified.
317 if(dc
->w
.flags
== DC_MEMORY
)
320 DbgPrint("win32k: realizepalette unimplemented step 2 for DC_MEMORY");
322 if(SurfGDI
->SetPalette
)
324 IntLockGDIDriver(SurfGDI
);
325 ASSERT(sysGDI
->NumColors
<= 256);
326 success
= SurfGDI
->SetPalette(dc
->PDev
, sysPtr
, 0, 0, sysGDI
->NumColors
);
327 IntUnLockGDIDriver(SurfGDI
);
331 // need to pass this to IntEngCreateXlate with palettes unlocked
332 sysMode
= sysGDI
->Mode
;
333 palMode
= palGDI
->Mode
;
334 PALETTE_UnlockPalette(systemPalette
);
335 PALETTE_UnlockPalette(dc
->w
.hPalette
);
337 // Step 3: Create the XLATEOBJ for device managed DCs
338 if(dc
->w
.flags
!= DC_MEMORY
)
341 palGDI
->logicalToSystem
= IntEngCreateXlate(sysGDI
->Mode
, palGDI
->Mode
, systemPalette
, dc
->w
.hPalette
);
349 BOOL STDCALL
NtGdiResizePalette(HPALETTE hpal
,
352 /* PALOBJ *palPtr = (PALOBJ*)AccessUserObject(hPal);
353 UINT cPrevEnt, prevVer;
354 INT prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
355 XLATEOBJ *XlateObj = NULL;
357 if(!palPtr) return FALSE;
358 cPrevEnt = palPtr->logpalette->palNumEntries;
359 prevVer = palPtr->logpalette->palVersion;
360 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) + sizeof(int*) + sizeof(GDIOBJHDR);
361 size += sizeof(int*) + sizeof(GDIOBJHDR);
362 XlateObj = palPtr->logicalToSystem;
364 if (!(palPtr = GDI_ReallocObject(size, hPal, palPtr))) return FALSE;
368 XLATEOBJ *NewXlateObj = (int*) HeapReAlloc(GetProcessHeap(), 0, XlateObj, cEntries * sizeof(int));
369 if(NewXlateObj == NULL)
371 ERR("Can not resize logicalToSystem -- out of memory!");
372 GDI_ReleaseObj( hPal );
375 palPtr->logicalToSystem = NewXlateObj;
378 if(cEntries > cPrevEnt)
380 if(XlateObj) memset(palPtr->logicalToSystem + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
381 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
382 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize), cEntries - cPrevEnt );
384 palPtr->logpalette->palNumEntries = cEntries;
385 palPtr->logpalette->palVersion = prevVer;
386 // GDI_ReleaseObj( hPal );
393 * Select logical palette into device context.
394 * \param hDC handle to the device context
395 * \param hpal handle to the palette
396 * \param ForceBackground If this value is FALSE the logical palette will be copied to the device palette only when the applicatioon
397 * is in the foreground. If this value is TRUE then map the colors in the logical palette to the device
398 * palette colors in the best way.
399 * \return old palette
401 * \todo implement ForceBackground == TRUE
403 HPALETTE STDCALL
NtGdiSelectPalette(HDC hDC
,
405 BOOL ForceBackground
)
411 // FIXME: mark the palette as a [fore\back]ground pal
415 /* Check if this is a valid palette handle */
416 PalGDI
= PALETTE_LockPalette(hpal
);
419 PALETTE_UnlockPalette(hpal
);
420 oldPal
= dc
->w
.hPalette
;
421 dc
->w
.hPalette
= hpal
;
433 BOOL STDCALL
NtGdiSetColorAdjustment(HDC hDC
,
434 CONST LPCOLORADJUSTMENT ca
)
439 UINT STDCALL
NtGdiSetPaletteEntries(HPALETTE hpal
,
442 CONST LPPALETTEENTRY pe
)
447 palGDI
= PALETTE_LockPalette(hpal
);
448 if (!palGDI
) return 0;
450 numEntries
= palGDI
->NumColors
;
451 if (Start
>= numEntries
)
453 PALETTE_UnlockPalette(hpal
);
456 if (numEntries
< Start
+ Entries
)
458 Entries
= numEntries
- Start
;
460 memcpy(&palGDI
->IndexedColors
[Start
], pe
, Entries
* sizeof(PALETTEENTRY
));
461 PALETTE_ValidateFlags(palGDI
->IndexedColors
, palGDI
->NumColors
);
462 ExFreePool(palGDI
->logicalToSystem
);
463 palGDI
->logicalToSystem
= NULL
;
464 PALETTE_UnlockPalette(hpal
);
470 NtGdiSetSystemPaletteUse(HDC hDC
, UINT Usage
)
476 NtGdiUnrealizeObject(HGDIOBJ hgdiobj
)
482 NtGdiUpdateColors(HDC hDC
)
486 hWnd
= IntWindowFromDC(hDC
);
489 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
492 return NtUserRedrawWindow(hWnd
, NULL
, 0, RDW_INVALIDATE
);
495 INT STDCALL
COLOR_PaletteLookupPixel(PALETTEENTRY
*palPalEntry
, INT size
,
496 XLATEOBJ
*XlateObj
, COLORREF col
, BOOL skipReserved
)
498 int i
, best
= 0, diff
= 0x7fffffff;
501 for( i
= 0; i
< size
&& diff
; i
++ )
503 if(!(palPalEntry
[i
].peFlags
& PC_SYS_USED
) || (skipReserved
&& palPalEntry
[i
].peFlags
& PC_SYS_RESERVED
))
506 r
= palPalEntry
[i
].peRed
- GetRValue(col
);
507 g
= palPalEntry
[i
].peGreen
- GetGValue(col
);
508 b
= palPalEntry
[i
].peBlue
- GetBValue(col
);
512 if( r
< diff
) { best
= i
; diff
= r
; }
515 if (XlateObj
== NULL
)
518 return (XlateObj
->pulXlate
) ? (INT
)XlateObj
->pulXlate
[best
] : best
;
521 COLORREF STDCALL
COLOR_LookupNearestColor( PALETTEENTRY
* palPalEntry
, int size
, COLORREF color
)
526 index
= COLOR_PaletteLookupPixel(palPalEntry
, size
, NULL
, color
, FALSE
);
528 palPalEntry
[index
].peRed
,
529 palPalEntry
[index
].peGreen
,
530 palPalEntry
[index
].peBlue
);
532 unsigned char spec_type
= color
>> 24;
534 PALETTEENTRY
*COLOR_sysPal
= (PALETTEENTRY
*)ReturnSystemPalette();
536 // we need logical palette for PALETTERGB and PALETTEINDEX colorrefs
538 if( spec_type
== 2 ) /* PALETTERGB */
539 color
= *(COLORREF
*)(palPalEntry
+ COLOR_PaletteLookupPixel(palPalEntry
,size
,NULL
,color
,FALSE
));
541 else if( spec_type
== 1 ) /* PALETTEINDEX */
543 if( (i
= color
& 0x0000ffff) >= size
)
545 DbgPrint("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color
, i
);
546 color
= *(COLORREF
*)palPalEntry
;
548 else color
= *(COLORREF
*)(palPalEntry
+ i
);
552 return (0x00ffffff & *(COLORREF
*)(COLOR_sysPal
+ COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
, color
, FALSE
)));
556 int STDCALL
COLOR_PaletteLookupExactIndex( PALETTEENTRY
* palPalEntry
, int size
,
560 BYTE r
= GetRValue(col
), g
= GetGValue(col
), b
= GetBValue(col
);
561 for( i
= 0; i
< size
; i
++ )
563 if( palPalEntry
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
564 if(palPalEntry
[i
].peRed
== r
&& palPalEntry
[i
].peGreen
== g
&& palPalEntry
[i
].peBlue
== b
) return i
;