#ifndef lint static char sccsid[] = "@(#)color.c 9.2 88/01/19 Copyright 1985 Sun Micro"; #endif /* * Copyright (c) 1985 by Sun Microsystems, Inc. */ /*- Routines for dealing with color color.c, Wed Sep 25 10:29:56 1985 James Gosling, Sun Microsystems */ #include #ifdef REF #include #include #endif #include "shape.h" #include #include #include "colortbl.inc" #ifndef CAT #define IDENT(a) a #define CAT(a,b) IDENT(a)b #endif struct color cs_frhsbcolor(); struct color cs_frrgbcolor(); static initialized_colormaps; extern struct pixrectops tile_ops; extern struct color cs_frrgbcolor(); #ifdef undef #define gray_pix(pc) short CAT(image_,pc)[]; \ mpr_static (CAT(graytilerect_,pc), 16, 16, 1, CAT(image_,pc)); \ static struct pixrect CAT(graypixrect_,pc) = \ {&tile_ops, 0x7FFFFFFF, 0x7FFFFFFF, 1, (caddr_t) &CAT(graytilerect_,pc)}; \ static short CAT(image_,pc)[16] = #define gray_slot(pc) fraction(100-pc,100), &CAT(graypixrect_,pc) gray_pix(6) { #include "gray_6.pr" }; gray_pix(13) { #include "gray_13.pr" }; gray_pix(19) { #include "gray_19.pr" }; gray_pix(31) { #include "gray_31.pr" }; gray_pix(38) { #include "gray_38.pr" }; gray_pix(25) { #include "gray_25.pr" }; gray_pix(50) { #include "gray_50.pr" }; gray_pix(75) { #include "gray_75.pr" }; gray_pix(100) { #include "gray_100.pr" }; static struct graytable { fract graylevel; struct pixrect *pattern; } graytable[] = { gray_slot(6), gray_slot(13), gray_slot(19), gray_slot(25), gray_slot(31), gray_slot(38), gray_slot(50), gray_slot(75), gray_slot(100), }; #endif cs_setcolor(gc, color) register struct graphics_context *gc; struct color color; { gc->color = color; gc->pattern = 0; gc->func = pixrectopcode(gc->canvas,&color); } #ifdef undef cs_frsetrgbcolor(gc, red, green, blue) register struct graphics_context *gc; fract red, green, blue; { gc->color = cs_frrgbcolor(red, green, blue); if (gc->canvas == 0) gc->func = PIX_SET; else if (gc->canvas->pr_depth == 1) { /* Black and white */ register fract level = max(red, green); level = max(level, blue); if (level <= fracti(0)) { gc->pattern = 0; if (level == fracti(-1)) gc->func = PIX_NOT(PIX_DST); else gc->func = PIX_SET; } else if (level > graytable[0].graylevel) { gc->func = PIX_CLR; gc->pattern = 0; } else { register struct graytable *p = graytable + 1; while (level < p->graylevel) p++; gc->func = PIX_SRC; gc->pattern = p[-1].pattern; } } else { /* The real thing! */ if (!initialized_colormaps) cs_initialize_colormaps(gc->canvas); if (red < 0) gc->func = PIX_NOT(PIX_DST); else if (red == green && green == blue) { register t = (red*255)>>16; gc->func = PIX_COLOR(grey_inverse[t <= 0 ? 0 : t > 255 ? 255 : t]) | PIX_SRC; } else gc->func = PIX_COLOR(red_inverse[floorfr(red * 255)] + green_inverse[floorfr(green * 255)] + blue_inverse[floorfr(blue * 255)]) | PIX_SRC; gc->pattern = 0; } } #endif cs_overlaydraw(gc) register struct graphics_context *gc; { gc->func = PIX_NOT(PIX_DST); } cs_overlayerase(gc) register struct graphics_context *gc; { gc->func = PIX_NOT(PIX_DST); } #ifdef undef cs_frsethsbcolor(gc, hue, saturation, brightness) register struct graphics_context *gc; fract hue, saturation, brightness; { cs_setcolor(gc, cs_frhsbcolor(hue, saturation, brightness)); } #endif fract cs_frcurrentgray(gc) register struct graphics_context *gc; { register v = gc->color.red; if (gc->color.green > v) v = gc->color.green; if (gc->color.blue > v) v = gc->color.blue; return fraction(v, 255); } cs_contrastswithcurrent(gc, c) register struct graphics_context *gc; struct color c; { if (gc->canvas->pr_depth>1) return c.hint != gc->color.hint; return c.hint != 0 ? gc->color.hint == 0 : gc->color.hint != 0; } cs_initialize_colormaps(fb) register struct pixrect *fb; { if (fb->pr_depth == 8) { register i; static all = ~0; pr_putcolormap(fb, 0, 256, red_map, green_map, blue_map); pr_putattributes(fb, &all); initialized_colormaps = 1; } }