📄 color.c
字号:
} if (i != ColorMapNMax) colorMapCur = i; } } /* Get the colormap name from its id */char *GetColorMapName (unsigned long cm) { unsigned int i; char flagInverse; char *str; i = cm>>ColorMapShift; flagInverse = i & ColorMapInverseMask; i = i & ColorMapMask; if (i>=ColorMapNMax || i == 0) Errorf("GetColorMapName() : Bad colormap id '%d'",i); if (theColorMaps[i] == NULL || theColorMaps[i]->name == NULL) Errorf("GetColorMapName() : Bad colormap id '%d'",i); if (!flagInverse) return(theColorMaps[i]->name); str = CharAlloc(strlen(theColorMaps[i]->name)+2); strcpy(str,"_"); strcpy(str+1,theColorMaps[i]->name); TempPtr(str); return(str);} /* Modifying (if it exists) or creating a colormap named "name" so that it will have "nb" colors */static int CGetColorMap(char *name, int nb){ int cm,i; if (nb <= 0) Errorf("CGetColorMap() : 'nb' should be positive"); if (name != NULL && *name == '_') Errorf("CGetColorMap() : Sorry, you cannot create a color map whose name starts with a '_'"); cm = GetColorMap(name); /* If Colormap already exists we delete it */ if (cm != -1) { for (i=0;i<theColorMaps[cm]->size;i++) { if (theColorMaps[cm]->colors[i].name != NULL) { Free(theColorMaps[cm]->colors[i].name); theColorMaps[cm]->colors[i].name = NULL; } } if (theColorMaps[cm]->colors != NULL) { Free(theColorMaps[cm]->colors); theColorMaps[cm]->colors = NULL; } } /* Otherwise we must create the structure */ else { for (i=0;i<ColorMapNMax;i++) { if (theColorMaps[i] == NULL) break; } if (i == ColorMapNMax) Errorf("CGetColorMap() : Sorry, too many colormaps (maximum is %d)",ColorMapNMax); cm = i; theColorMaps[cm] = (COLORMAP) Malloc(sizeof(struct colorMap)); theColorMaps[cm]->colors = NULL; theColorMaps[cm]->size = 0; if (name != NULL) { theColorMaps[cm]->name = CopyStr(name); nColorMaps++; } } /* Then we allocate the array of colors */ if (nb != 0) { theColorMaps[cm]->colors = (Color *) Malloc(sizeof(struct color)*nb); theColorMaps[cm]->size = nb; for (i=0;i<nb;i++) { theColorMaps[cm]->colors[i].name = NULL; theColorMaps[cm]->colors[i].pixel = 0; } } return(cm);} /* Get the number of colors of a given colormap */int ColorMapSize(unsigned long colorMap){ int cm = (colorMap>>ColorMapShift)&ColorMapMask; if (theColorMaps[cm]==NULL) Errorf("ColorMapSize() : Bad colormap"); return(theColorMaps[cm]->size);}/*********************************************************************************** * * Dealing with pixels * ***********************************************************************************/unsigned long Color2Pixel(unsigned long color){ unsigned long c; int cm; char flagInverse; if (color & invisibleColor) Errorf("Color2Pixel() : Unexpected 'invisible' color"); c = color & 0xFFFFFF; cm = color >> ColorMapShift; flagInverse = cm&ColorMapInverseMask; cm = cm&ColorMapMask; if (theColorMaps[cm] == NULL) Errorf("Bad Color number"); if (theColorMaps[cm]->size <= c) Errorf("Bad Color number"); if (flagInverse) c = theColorMaps[cm]->size-c-1; return(theColorMaps[cm]->colors[c].pixel);} void Color2RGB(unsigned long color, unsigned short *r,unsigned short *g,unsigned short *b){ unsigned long c; int cm; char flagInverse; if (color & invisibleColor) Errorf("Color2RGB() : Unexpected 'invisible' color"); c = color & 0xFFFFFF; cm = color >> ColorMapShift; flagInverse = cm&ColorMapInverseMask; cm = cm&ColorMapMask; if (theColorMaps[cm] == NULL) Errorf("Bad Color map number"); if (theColorMaps[cm]->size <= c) Errorf("Bad Color %d number"); if (flagInverse) c = theColorMaps[cm]->size-c-1; *r = theColorMaps[cm]->colors[c].red; *g = theColorMaps[cm]->colors[c].green; *b = theColorMaps[cm]->colors[c].blue;} char *GetColorName(unsigned long color){ unsigned long c; int cm; static char str[50]; char flagInverse; if (color & invisibleColor) return("invisible"); c = color & 0xFFFFFF; cm = color >> ColorMapShift; flagInverse = cm&ColorMapInverseMask; cm = cm&ColorMapMask; if (theColorMaps[cm] == NULL) Errorf("Bad Color number"); if (theColorMaps[cm]->size <= c) Errorf("Bad Color number"); if (cm == 0) return(theColorMaps[cm]->colors[c].name); if (flagInverse) sprintf(str,"%ld_%s",c,theColorMaps[cm]->colors[c].name); else sprintf(str,"%ld%s",c,theColorMaps[cm]->colors[c].name); return(str);} /*********************************************************************************** * * Parsing a colormap name * ***********************************************************************************/ unsigned long GetColorMapCur(void){ return(colorMapCur<<ColorMapShift); } /* Get a colormap from string 'arg' which has already been evaluated */ static char ParseColorMapInt_(char *arg, int defVal, int *colormap){ int cm; char flagInverse; *colormap = defVal; if (arg == NULL) { SetErrorf("ParseColorMap__() : NULL string cannot be converted to a color"); return(NO); } if (*arg == '_') { if (*(arg+1) == '\0') { if (colorMapCur&ColorMapInverseMask) *colormap = (colorMapCur-ColorMapInverseMask) ; else *colormap = (colorMapCur+ColorMapInverseMask); return(YES); } flagInverse = YES; arg++; } else flagInverse = NO; cm = GetColorMap(arg); if (cm == -1) { SetErrorf("ParseColorMap_() : Bad colormap name '%s'",arg); return(NO); } *colormap = cm; if (flagInverse) *colormap += ColorMapInverseMask; return(YES);}static void ParseColorMapInt(char *arg, int *colormap){ if (ParseColorMapInt_(arg,0,colormap) == NO) Errorf1("");}/* Parse a color map, with evaluation */char ParseColorMap_(char *arg, unsigned long defVal, unsigned long *colormap){ int cm; char *str; *colormap = defVal; if (ParseStr_(arg,NULL,&str) == NO) return(NO); if (ParseColorMapInt_(str,-1,&cm) == NO) return(NO); *colormap = cm << ColorMapShift; return(YES);}void ParseColorMap(char *arg, unsigned long *colormap){ if (ParseColorMap_(arg,0,colormap) == NO) Errorf1("");}/*********************************************************************************** * * Parsing a color name * ***********************************************************************************/ char ParseColor_(char *arg, unsigned long defVal, unsigned long *color){ char *endp; unsigned long l; int cm,i; VALUE val; LWFLOAT f; char *str; val = NULL; if (!ParseFloatStrLevel_(levelCur,arg,&f,&str)) { *color = defVal; return(NO); } *color = defVal; if (arg == NULL) { SetErrorf("ParseColor_() : NULL string cannot be converted to a color"); return(NO); } /* Case of a string */ if (str != NULL) { /* Particular case : FG and BG, invisible */ if (!strcmp("FG",str)) { *color = fgColor; return(YES); } if (!strcmp("BG",str)) { *color = bgColor; return(YES); } if (!strcmp("invisible",str)) { *color = invisibleColor; return(YES); } /* Otherwise let's try to read a number and colormap afterwards */ endp = NULL; l = strtoul(str,&endp,10); /* If we could not then it must be a symbolic name */ if (endp == str) { i = GetNamedColor(str); if (i == -1 || theColorMaps[0]->colors[i].name == NULL) { SetErrorf("ParseColor_() : Unknown color %s",arg); return(NO); } *color = i; return(YES); } /* Otherwise, we got the number let's get a colormap name */ if (!ParseColorMapInt_(endp,0,&cm)) { SetErrorf("ParseColor_() : Bad colormap name '%s'",endp); return(NO); } } /* Case of a number (default colomap is used) */ else { if (f != (int) f || f < 0) { SetErrorf("ParseColor_() : '%g' is not a color",f); return(NO); } l = (int) f; cm = colorMapCur; if (theColorMaps[cm] == NULL) { SetErrorf("ParseColor_() : Bad current colormap"); return(NO); } } /* Then just get the color 'l' associated to the colormap 'cm' */ if (l >= theColorMaps[cm]->size) { SetErrorf("ParseColor_() : Bad index color '%d'",l); return(NO); } *color = (((unsigned long) cm) << 25) + l; return(YES);} void ParseColor(char *arg, unsigned long *color){ if (ParseColor_(arg,0,color) == NO) Errorf1("");}/******************************************************************* * * Build the colormap * *******************************************************************/ #define MaxNColors 4000int BuildColormap(char flagShared, char mouseMode){ static unsigned short red[MaxNColors], green[MaxNColors], blue[MaxNColors]; static unsigned long pixels[MaxNColors]; int nColors,ncm,i,j,k,last; int nAllocColors; nColors = 0; ncm = nColorMaps+1; /* * In the case the Depth is greater than 8 we do not check * wether two colors are the same. Otherwise we have * to check it. */ if (WDepth() > 8) { for (i=0;i<ColorMapNMax;i++) { if (ncm == 0) break; if (theColorMaps[i] != NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -