3 * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
6 * This file is a component of an X Window System-specific implementation
7 * of Xcms based on the TekColor Color Management System. TekColor is a
8 * trademark of Tektronix, Inc. The term "TekHVC" designates a particular
9 * color space that is the subject of U.S. Patent No. 4,985,853 (equivalent
10 * foreign patents pending). Permission is hereby granted to use, copy,
11 * modify, sell, and otherwise distribute this software and its
12 * documentation for any purpose and without fee, provided that:
14 * 1. This copyright, permission, and disclaimer notice is reproduced in
15 * all copies of this software and any modification thereof and in
16 * supporting documentation;
17 * 2. Any color-handling application which displays TekHVC color
18 * cooordinates identifies these as TekHVC color coordinates in any
19 * interface that displays these coordinates and in any associated
21 * 3. The term "TekHVC" is always used, and is only used, in association
22 * with the mathematical derivations of the TekHVC Color Space,
23 * including those provided in this file and any equivalent pathways and
24 * mathematical derivations, regardless of digital (e.g., floating point
25 * or integer) representation.
27 * Tektronix makes no representation about the suitability of this software
28 * for any purpose. It is provided "as is" and with all faults.
30 * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
31 * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32 * PARTICULAR PURPOSE. IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
33 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
34 * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
35 * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
36 * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
42 * Source for the XcmsTekHVCQueryMaxV() gamut boundary
58 #define MAXBISECTCOUNT 100
62 /************************************************************************
66 ************************************************************************/
70 * XcmsTekHVCQueryMaxV - Compute maximum value for a hue and chroma
79 XcmsColor *pColor_return)
82 * Return the maximum value for a specified hue and chroma.
85 * This routine assumes that the white point associated with
86 * the color specification is the Screen White Point. The
87 * Screen White Point will also be associated with the
88 * returned color specification.
91 * XcmsFailure - Failure
92 * XcmsSuccess - Succeeded with no modifications
100 int nCount, nMaxCount;
101 XcmsFloat nT, nChroma, savedChroma, lastValue, lastChroma, prevChroma;
103 XcmsFloat ftmp1, ftmp2;
108 if (ccc == NULL || pColor_return == NULL) {
113 * Insure TekHVC installed
115 if (XcmsAddColorSpace(&XcmsTekHVCColorSpace) == XcmsFailure) {
119 /* setup the CCC to use for the conversions. */
120 memcpy ((char *) &myCCC, (char *) ccc, sizeof(XcmsCCCRec));
121 myCCC.clientWhitePt.format = XcmsUndefinedFormat;
122 myCCC.gamutCompProc = (XcmsCompressionProc) NULL;
124 tmp.spec.TekHVC.H = hue;
125 tmp.spec.TekHVC.V = 0.0;
126 tmp.spec.TekHVC.C = chroma;
127 tmp.pixel = pColor_return->pixel;
128 tmp.format = XcmsTekHVCFormat;
130 if (!_XcmsTekHVC_CheckModify (&tmp)) {
134 /* Step 1: compute the maximum value and chroma for this hue. */
135 /* This copy may be overkill but it preserves the pixel etc. */
136 memcpy((char *)&max_vc, (char *)&tmp, sizeof(XcmsColor));
137 hue = max_vc.spec.TekHVC.H;
138 if (_XcmsTekHVCQueryMaxVCRGB(&myCCC, max_vc.spec.TekHVC.H, &max_vc, &rgb_saved)
143 if (max_vc.spec.TekHVC.C < tmp.spec.TekHVC.C) {
145 * If the chroma is greater than the chroma for the
146 * maximum value/chroma point then the value is the
147 * the value for the maximum value, chroma point.
148 * This is an error but it I return the best approximation I can.
149 * Thus the inconsistency.
151 tmp.spec.TekHVC.C = max_vc.spec.TekHVC.C;
152 tmp.spec.TekHVC.V = max_vc.spec.TekHVC.V;
153 memcpy ((char *) pColor_return, (char *) &tmp, sizeof (XcmsColor));
155 } else if (max_vc.spec.TekHVC.C == tmp.spec.TekHVC.C) {
157 * If the chroma is equal to the chroma for the
158 * maximum value/chroma point then the value is the
159 * the value for the maximum value, chroma point.
161 tmp.spec.TekHVC.V = max_vc.spec.TekHVC.V;
162 memcpy ((char *) pColor_return, (char *) &tmp, sizeof (XcmsColor));
165 /* must do a bisection here to compute the maximum value */
166 /* save the structure input so that any elements that */
167 /* are not touched are recopied later in the routine. */
168 nChroma = savedChroma = tmp.spec.TekHVC.C;
169 tmp.spec.TekHVC.C = max_vc.spec.TekHVC.C;
170 tmp.spec.TekHVC.V = max_vc.spec.TekHVC.V;
173 nMaxCount = MAXBISECTCOUNT;
176 for (nCount = 0; nCount < nMaxCount; nCount++) {
177 prevChroma = lastChroma;
178 lastValue = tmp.spec.TekHVC.V;
179 lastChroma = tmp.spec.TekHVC.C;
180 nT = (1.0 - (nChroma / max_vc.spec.TekHVC.C)) * rFactor;
181 tmp.spec.RGBi.red = rgb_saved.red * (1.0 - nT) + nT;
182 tmp.spec.RGBi.green = rgb_saved.green * (1.0 - nT) + nT;
183 tmp.spec.RGBi.blue = rgb_saved.blue * (1.0 - nT) + nT;
184 tmp.format = XcmsRGBiFormat;
186 /* convert from RGB to HVC */
187 if (_XcmsConvertColorsWithWhitePt(&myCCC, &tmp,
188 &myCCC.pPerScrnInfo->screenWhitePt, 1, XcmsTekHVCFormat,
189 (Bool *) NULL) == XcmsFailure) {
193 /* Now check the return against what is expected */
194 if (tmp.spec.TekHVC.C <= savedChroma + EPS &&
195 tmp.spec.TekHVC.C >= savedChroma - EPS) {
196 tmp.spec.TekHVC.H = hue; /* use the saved hue */
197 memcpy ((char *) pColor_return, (char *) &tmp, sizeof (XcmsColor));
200 nChroma += savedChroma - tmp.spec.TekHVC.C;
201 if (nChroma > max_vc.spec.TekHVC.C) {
202 nChroma = max_vc.spec.TekHVC.C;
203 rFactor *= 0.5; /* selective relaxation employed */
204 } else if (nChroma < 0.0) {
205 /* avoid using fabs */
206 ftmp1 = lastChroma - savedChroma;
209 ftmp2 = tmp.spec.TekHVC.C - savedChroma;
213 tmp.spec.TekHVC.V = lastValue;
214 tmp.spec.TekHVC.C = lastChroma;
216 /* make sure to return the input hue */
217 tmp.spec.TekHVC.H = hue;
218 if (!_XcmsTekHVC_CheckModify(&tmp)) {
221 memcpy ((char *) pColor_return, (char *) &tmp, sizeof (XcmsColor));
223 } else if (tmp.spec.TekHVC.C <= prevChroma + EPS &&
224 tmp.spec.TekHVC.C >= prevChroma - EPS) {
225 rFactor *= 0.5; /* selective relaxation employed */
228 if (nCount >= nMaxCount) {
229 /* avoid using fabs */
230 ftmp1 = lastChroma - savedChroma;
233 ftmp2 = tmp.spec.TekHVC.C - savedChroma;
237 tmp.spec.TekHVC.V = lastValue;
238 tmp.spec.TekHVC.C = lastChroma;
243 /* make sure to return the input hue */
244 tmp.spec.TekHVC.H = hue;
245 memcpy ((char *) pColor_return, (char *) &tmp, sizeof (XcmsColor));