-+-+-+-+-+-+-+-+ START OF PART 196 -+-+-+-+-+-+-+-+ Xextern void ptk_hsltorgb(C(Pcobundl *) hsl, C(Pcobundl *) rgb) XPreANSI(Pcobundl *hsl) XPreANSI(Pcobundl *rgb) X/* X** \parambegin X** \param`7BPcobundl *`7D`7Bhsl`7D`7BHSL triplet`7D`7BIN`7D X** \param`7BPcobundl *`7D`7Brgb`7D`7BRGB triplet`7D`7BIN`7D X** \paramend X** \blurb`7BThis function converts from the HSL double-ended hexcone`20 X** model to the`20 X** RGB model. Given HSL, the equivalent RGB parameters are computed. X** All parameters are assumed to be in the range 0.0 to 1.0. The X** algorithm is adapted from`7E\cite`7Bfoley:fic`7D.`7D X*/ X`7B X Pfloat value, m1, m2; X Pfloat ahsl`5B3`5D, argb`5B3`5D; X X value = 1.0/3.0; X X ptk_cobundltoreal(ahsl, hsl, 2); X if (ahsl`5B2`5D <= 0.5)`20 X m2 = ahsl`5B2`5D * (1.0 + ahsl`5B1`5D); X else X m2 = ahsl`5B2`5D + ahsl`5B1`5D - (ahsl`5B2`5D * ahsl`5B1`5D); X X m1 = 2.0 * ahsl`5B2`5D - m2; X if (ahsl`5B1`5D == 0.0)`20 X `7B X argb`5B0`5D = ahsl`5B2`5D; X argb`5B1`5D = ahsl`5B2`5D; X argb`5B2`5D = ahsl`5B2`5D; X `7D X else X `7B `20 X argb`5B0`5D = ptk_rgbv(m1, m2, ahsl`5B0`5D + value); X argb`5B1`5D = ptk_rgbv(m1, m2, ahsl`5B0`5D); X argb`5B2`5D = ptk_rgbv(m1, m2, ahsl`5B0`5D - value); X `7D X ptk_realtocobundl(argb, rgb, 1); X`7D /* ptk_hsltorgb */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern void ptk_rgbtohsl(C(Pcobundl *) rgb, C(Pcobundl *) hsl) XPreANSI(Pcobundl *rgb) XPreANSI(Pcobundl *hsl) X/* X** \parambegin X** \param`7BPcobundl *`7D`7Brgb`7D`7BRGB triplet`7D`7BIN`7D X** \param`7BPcobundl *`7D`7Bhsl`7D`7BHSL triplet`7D`7BIN`7D X** \paramend X** \blurb`7BThis function converts an RGB triplet to a HSL triplet. X** The X** algorithm is adapted from`7E\cite`7Bwatt:fotdcg`7D.`7D X*/ X`7B X Pfloat maxval, minval, diff, rdist, gdist, bdist; X Pfloat ahsl`5B3`5D, argb`5B3`5D; X X ptk_cobundltoreal(argb, rgb, 1); X maxval = MAX(argb`5B0`5D, argb`5B1`5D); X maxval = MAX(maxval, argb`5B2`5D); X minval = MIN(argb`5B0`5D, argb`5B1`5D); X minval = MIN(minval, argb`5B2`5D); X diff = maxval - minval; X ahsl`5B2`5D = (maxval + minval) / 2.0; X if (abs(diff) < 0.00001) X `7B `20 X ahsl`5B1`5D = 0.0; X ahsl`5B0`5D = 0.0; /* undefined */ X `7D X else X `7B X if (ahsl`5B2`5D <= 0.5) X ahsl`5B1`5D = diff / (maxval + minval); X else X ahsl`5B1`5D = diff / (2.0 - maxval - minval); X rdist = (maxval - argb`5B0`5D)/diff; X gdist = (maxval - argb`5B1`5D)/diff; X bdist = (maxval - argb`5B2`5D)/diff; X if (argb`5B0`5D == maxval) X ahsl`5B0`5D = bdist - gdist; X else X if (argb`5B1`5D == maxval) X ahsl`5B0`5D = 2.0 + rdist - bdist; X else X if (argb`5B2`5D == maxval) X ahsl`5B0`5D = 4.0 + gdist - rdist; X ahsl`5B0`5D *= 60.0; X if (ahsl`5B0`5D < 0.0) X ahsl`5B0`5D += 360.0; X `7D `20 X ptk_realtocobundl(ahsl, hsl, 2); X`7D /* ptk_rgbtohsl */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern void ptk_hsvtorgb(C(Pcobundl *) hsv, C(Pcobundl *) rgb) XPreANSI(Pcobundl *hsv) XPreANSI(Pcobundl *rgb) X/* X** \parambegin X** \param`7BPcobundl *`7D`7Bhvs`7D`7BHSV triplet`7D`7BIN`7D X** \param`7BPcobundl *`7D`7Brgb`7D`7BRGB triplet`7D`7BIN`7D X** \paramend X** \blurb`7BThis function converts a HSV triplet to a RGB triplet.`20 X** The X** algorithm is adapted from`7E\cite`7Bwatt:fotdcg`7D.`7D X*/ X`7B X Pfloat f, p, q, t; X Pint i; X Pfloat ahsv`5B3`5D, argb`5B3`5D; X X ptk_cobundltoreal(ahsv, hsv, 3); X if (ahsv`5B1`5D == 0.0) X `7B X argb`5B0`5D = ahsv`5B2`5D; X argb`5B1`5D = ahsv`5B2`5D; X argb`5B2`5D = ahsv`5B2`5D; X `7D X else X `7B X if (ahsv`5B0`5D == 1.0) X ahsv`5B0`5D = 0.0; X ahsv`5B0`5D *= 6.0; X i = floor(ahsv`5B0`5D); X f = ahsv`5B0`5D - (Pfloat)i; X p = ahsv`5B2`5D * (1.0 - ahsv`5B1`5D); X q = ahsv`5B2`5D * (1.0 - (ahsv`5B1`5D * f)); X t = ahsv`5B2`5D * (1.0 - (ahsv`5B1`5D * (1.0 - f))); X switch (i)`20 X `7B X case 0: argb`5B0`5D = ahsv`5B2`5D; X argb`5B1`5D = t; X argb`5B2`5D = p; X break; X case 1: argb`5B0`5D = q; X argb`5B1`5D = ahsv`5B2`5D; X argb`5B2`5D = p; X break; X case 2: argb`5B0`5D = p; X argb`5B1`5D = ahsv`5B2`5D; X argb`5B2`5D = t; X break; X case 3: argb`5B0`5D = p; X argb`5B1`5D = q; X argb`5B2`5D = ahsv`5B2`5D; X break; X case 4: argb`5B0`5D = t; X argb`5B1`5D = p; X argb`5B2`5D = ahsv`5B2`5D; X break; X case 5: argb`5B0`5D = ahsv`5B2`5D; X argb`5B1`5D = p; X argb`5B2`5D = q; X break; X `7D X `7D X ptk_realtocobundl(argb, rgb, 1); X`7D /* ptk_hsvtorgb */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern void ptk_rgbtohsv(C(Pcobundl *) rgb, C(Pcobundl *) hsv) XPreANSI(Pcobundl *rgb) XPreANSI(Pcobundl *hsv) X/* X** \parambegin X** \param`7BPcobundl *`7D`7Brgb`7D`7BRGB triplet`7D`7BIN`7D X** \param`7BPcobundl *`7D`7Bhsv`7D`7BHSV triplet`7D`7BIN`7D X** \paramend X** \blurb`7BThis function converts an RGB value to a HSV value. X** The X** algorithm is adapted from`7E\cite`7Bwatt:fotdcg`7D.`7D X*/ X`7B X Pfloat maxval, minval, diff, rdist, gdist, bdist; X Pfloat ahsv`5B3`5D, argb`5B3`5D; X X ptk_cobundltoreal(argb, rgb, 1); X maxval = MAX(argb`5B0`5D, argb`5B1`5D); X maxval = MAX(maxval, argb`5B2`5D); X minval = MIN(argb`5B0`5D, argb`5B1`5D); X minval = MIN(minval, argb`5B2`5D); X diff = maxval - minval; X ahsv`5B2`5D = maxval; X if (maxval != 0.0) X ahsv`5B1`5D = diff/maxval; X else X ahsv`5B1`5D = 0.0; X if (ahsv`5B1`5D == 0.0) X ahsv`5B0`5D = 0.0; /* undefined */ X else X `7B X rdist = (maxval - argb`5B0`5D)/diff; X gdist = (maxval - argb`5B1`5D)/diff; X bdist = (maxval - argb`5B2`5D)/diff; X if (argb`5B0`5D == maxval) X ahsv`5B0`5D = bdist - gdist; X else X if (argb`5B1`5D == maxval) X ahsv`5B0`5D = 2.0 + rdist - bdist; X else X if (argb`5B2`5D == maxval) X ahsv`5B0`5D = 4.0 + gdist - rdist; X ahsv`5B0`5D *= 60.0; X if (ahsv`5B0`5D < 0.0) X ahsv`5B0`5D += 360.0; X `7D X ptk_realtocobundl(ahsv, hsv, 3); X`7D /* ptk_rgbtohsv */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern ptkboolean ptk_cnstorgb(C(Pchar *) colourname, C(Pcobundl *) rgb) XPreANSI(Pchar *colourname) XPreANSI(Pcobundl *rgb) X/* X** \parambegin X** \param`7BPchar *`7D`7Bcolourname`7D`7Bcolour description`7D`7BIN`7D X** \param`7BPcobundl *`7D`7Brgb`7D`7BRGB triplet`7D`7BIN`7D X** \paramend X** \blurb`7BThis function converts a CNS colour name to the equivalent X** RGB value, returning TRUE if the conversion was successful, X** and FALSE if not.`7D X*/ X`7B X Pint err; X X convertcolourname(colourname, rgb, &err); X if ((err == 0) `7C`7C (err == 2)) X return TRUE; X else X `7B X return FALSE; X `7D X`7D /* ptk_cnstorgb */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern ptkboolean ptk_cnstohsl(C(Pchar *) colourname, C(Pcobundl *) hsl) XPreANSI(Pchar *colourname) XPreANSI(Pcobundl *hsl) X/* X** \parambegin X** \param`7BPchar *`7D`7Bcolourname`7D`7Bcolour description`7D`7BIN`7D X** \param`7BPcobundl *`7D`7Bhsl`7D`7BHSL triplet`7D`7BIN`7D X** \paramend X** \blurb`7BThis function converts a CNS colour name to the equivalent X** HSL value, returning TRUE if the conversion was successful, X** and FALSE if not.`7D X*/ X`7B X Pcobundl rgb; X X if (ptk_cnstorgb(colourname, &rgb)) X `7B X ptk_rgbtohsl(&rgb, hsl); X return TRUE; X `7D X else X return FALSE; X`7D /* ptk_cnstohsl */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern ptkboolean ptk_cnstohsv(C(Pchar *) colourname, C(Pcobundl *) hsv) XPreANSI(Pchar *colourname) XPreANSI(Pcobundl *hsv) X/* X** \parambegin X** \param`7BPchar *`7D`7Bcolourname`7D`7Bcolour description`7D`7BIN`7D X** \param`7BPcobundl *`7D`7Bhsv`7D`7BHSV triplet`7D`7BIN`7D X** \paramend X** \blurb`7BThis function Converts colour name to HSV. X** Returns TRUE if ok, FALSE if not ok.`7D X*/ X`7B X Pcobundl rgb; X X if (ptk_cnstorgb(colourname, &rgb)) X `7B X ptk_rgbtohsv(&rgb, hsv); X return TRUE; X `7D X else X return FALSE; X`7D /* ptk_cnstohsv */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern void ptk_setcnsdefaults(C(ptkelightness) lightness,`20 X C(ptkesaturation) saturation) XPreANSI(ptkelightness lightness) XPreANSI(ptkesaturation saturation) X/* X** \parambegin X** \param`7Bptkelightness`7D`7Blightness`7D`7Bdefault lightness for colours` V7D`7BIN`7D X** \param`7Bptkesaturation`7D`7Bsaturation`7D`7Bdefault saturation for colou Vrs`7D`7BIN`7D X** \paramend X** \blurb`7BThis function sets default values for lightness and X** saturation for the Colour Naming X** Scheme. If lightness or saturation is missing when a`20 X** colour name is subsequently specified, the X** default is used.`7D X*/ X`7B X if ((lightness >= 0) && (lightness <= 4)) X deflight = lightness; X if ((saturation >= 0) && (saturation <= 3)) X defsat = saturation + 5; X`7D /* ptk_setcnsdefaults */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern void ptk_inqcnsdefaults(C(ptkelightness *) lightness,`20 X C(ptkesaturation *) saturation) XPreANSI(ptkelightness *lightness) XPreANSI(ptkesaturation *saturation) X/* X** \parambegin X** \param`7Bptkelightness *`7D`7Blightness`7D`7Bdefault lightness for colour Vs`7D`7BOUT`7D X** \param`7Bptkesaturation *`7D`7Bsaturation`7D`7Bdefault saturation for col Vours`7D`7BOUT`7D X** \paramend X** \blurb`7BThis function inquires the X** default values of lightness and saturation used in the Colour X** Naming Scheme.`7D X*/ X`7B X *lightness = deflight; X *saturation = defsat - 5; X`7D /* ptk_inqcnsdefaults */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern void ptk_setupcolourtable(C(Pint) wsid, C(Pint) num, X C(Pchar **) colournames) XPreANSI(Pint wsid) XPreANSI(Pint num) XPreANSI(Pchar **colournames) X/* X** \parambegin X** \param`7BPint`7D`7Bwsid`7D`7Bworkstation identifier`7D`7BIN`7D X** \param`7BPint`7D`7Bnum`7D`7Bnumber of colour names`7D`7BIN`7D X** \param`7BPchar **`7D`7Bcolournames`7D`7Blist of colour names`7D`7BIN`7D X** \paramend X** \blurb`7BThis function sets colour representations in`20 X** the colour table of workstation \pardesc`7Bwsid`7D, X** using the list of colour names \pardesc`7Bcolournames`7D.`20 X** The hashstrings table X** `7B\tt "colourindex"`7D is used to derive the index to the colour table.` V7D X*/ X`7B X Pint i; X Pcobundl rep; X X for (i = 0; i < num; i++) X `7B X if (ptk_cnstorgb(colournames`5Bi`5D, &rep)) X psetcolourrep(wsid, ptk_stringtoint("colourindex", colournames`5Bi`5D) V,`20 X &rep); X `7D X`7D /* ptk_setupcolourtable */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern void ptk_setcolourrep(C(Pint) wsid, C(Pchar *) colourname) XPreANSI(Pint wsid) XPreANSI(Pchar *colourname) X/* X** \parambegin X** \param`7BPint`7D`7Bwsid`7D`7Bworkstation identifier`7D`7BIN`7D X** \param`7BPchar *`7D`7Bcolourname`7D`7Bcolour name`7D`7BIN`7D X** \paramend X** \blurb`7BThis function sets colour representations in`20 X** the colour table of workstation \pardesc`7Bwsid`7D, X** using the colour name \pardesc`7Bcolourname`7D.`20 X** The hashstrings table X** `7B\tt "colourindex"`7D is used to derive the index to the colour table.` V7D X*/ X`7B X Pcobundl rep; X X if (ptk_cnstorgb(colourname, &rep)) X psetcolourrep(wsid, ptk_stringtoint("colourindex", colourname), &rep); X`7D /* ptk_setcolourrep */ X X/*-------------------------------------------------------------------------- V*/ X X/*function:external*/ Xextern void ptk_setrgbcolourname(C(Pchar *) colourname, C(Pcobundl *) rgb) XPreANSI(Pchar *colourname) XPreANSI(Pcobundl *rgb) X/* X** \parambegin X** \param`7BPchar *`7D`7Bcolourname`7D`7Bcolour name`7D`7BIN`7D X** \param`7BPcobundl *`7D`7Brgb`7D`7BRGB colour value`7D`7BIN`7D X** \paramend X** \blurb`7BThis function sets a colour representation in`20 X** CNS using the colour name and X** RGB value. The colour name must be different to the names provided by X** the CNS. This function enables be additional names for colours to X** be specified in addition to those provided by CNS.`7D X*/ X`7B X Pint err, ii; X Pcobundl temp; X X convertcolourname(colourname, &temp, &err); X switch (err) X `7B X case 0: /* colourname is part of the colour naming scheme */ X fprintf(stderr, "ptk_setrgbcolourname: sorry, colourname \"%s\" is par Vt of the colour naming scheme\n", colourname); X break; X X case 1: /* blank line */ X fprintf(stderr, "ptk_setrgbcolourname: sorry, colourname \"%s\" is inv Valid\n", colourname); X break; X X case 2: /* already defined rgb name */ X ii = instrlist(rgbnames, numrgbs, colourname); X rgbvalues`5Bii`5D = *rgb; X break; X X case 3: /* name not rgb or cns */ X numrgbs++; X if (numrgbs == 1) X `7B X rgbnames = (Pchar **)calloc(1, sizeof(Pchar *)); X rgbvalues = (Pcobundl *)calloc(1, sizeof(Pcobundl)); X `7D X else X `7B X rgbnames = (Pchar **)realloc(rgbnames, numrgbs * sizeof(Pchar *)); X rgbvalues = (Pcobundl *)realloc(rgbvalues,`20 X numrgbs * sizeof(Pcobundl)); X `7D X rgbnames`5Bnumrgbs - 1`5D = (Pchar *)malloc(strlen(colourname) + 1); X strcpy(rgbnames`5Bnumrgbs - 1`5D, colourname); X rgbvalues`5Bnumrgbs - 1`5D = *rgb; X break; X `7D X`7D /* ptk_setrgbcolourname */ X +-+-+-+-+-+-+-+- END OF PART 196 +-+-+-+-+-+-+-+-