]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/parse_rgb.h
ncurses 6.1 - patch 20180908
[ncurses.git] / test / parse_rgb.h
1 /****************************************************************************
2  * Copyright (c) 2018 Free Software Foundation, Inc.                        *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: parse_rgb.h,v 1.4 2018/02/03 23:40:53 tom Exp $
30  *
31  * Sample implementation of ncurses RGB extension from user_caps(5).
32  */
33 #ifndef PARSE_RBG_H_incl
34 #define PARSE_RBG_H_incl 1
35
36 #include <test.priv.h>
37
38 #if HAVE_TIGETSTR && USE_WIDEC_SUPPORT
39 static int
40 parse_rgb(int *r_max, int *g_max, int *b_max)
41 {
42     int colors = tigetnum("colors");
43     int result = ERR;
44
45     *r_max = *g_max = *b_max = 0;
46
47     if (colors > 0) {
48         int max_bits;
49         int bits;
50         int pwr2;
51         int r = 0, g = 0, b = 0;
52         char *data;
53         char ch;
54
55         for (max_bits = 0, pwr2 = 1;
56              pwr2 < colors;
57              ++max_bits, pwr2 <<= 1) {
58             ;
59         }
60
61         if (tigetflag("RGB") > 0) {
62             result = OK;
63             r = g = b = (max_bits + 2) / 3;
64         } else if ((bits = tigetnum("RGB")) > 0) {
65             result = OK;
66             r = g = b = bits;
67         } else if ((data = tigetstr("RGB")) != ABSENT_STRING
68                    && data != CANCELLED_STRING
69                    && sscanf(data, "%d/%d/%d%c", &r, &g, &b, &ch) == 3) {
70             result = OK;
71         }
72
73         if ((r + g + b) < max_bits) {
74             result = ERR;
75         } else if (result == 0) {
76             if (r > max_bits) {
77                 r = max_bits;
78                 g = b = 0;
79             }
80             *r_max = r;
81             if (g > (max_bits -= r)) {
82                 g = max_bits;
83                 b = 0;
84             }
85             *g_max = g;
86             if (b > (max_bits -= g)) {
87                 b = max_bits;
88             }
89             *b_max = b;
90         }
91     }
92     return result;
93 }
94 #else
95 #define parse_rgb(r,g,b) (ERR)
96 #endif
97
98 #endif /* PARSE_RBG_H_incl */