source: libcaca/trunk/caca/attr.c @ 4145

Last change on this file since 4145 was 4145, checked in by Sam Hocevar, 10 years ago

Implement caca_unset_attr() and caca_toggle_attr(). Fixes #7.

  • Property svn:keywords set to Id
File size: 17.4 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: attr.c 4145 2009-12-18 21:36:15Z sam $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains functions for attribute management and colourspace
17 *  conversions.
18 */
19
20#include "config.h"
21
22#include "caca.h"
23#include "caca_internals.h"
24
25static uint8_t nearest_ansi(uint16_t);
26
27/* RGB colours for the ANSI palette. There is no real standard, so we
28 * use the same values as gnome-terminal. The 7th colour (brown) is a bit
29 * special: 0xfa50 instead of 0xfaa0. */
30static const uint16_t ansitab16[16] =
31{
32    0xf000, 0xf00a, 0xf0a0, 0xf0aa, 0xfa00, 0xfa0a, 0xfa50, 0xfaaa,
33    0xf555, 0xf55f, 0xf5f5, 0xf5ff, 0xff55, 0xff5f, 0xfff5, 0xffff,
34};
35
36/* Same table, except on 14 bits (3-4-4-3) */
37static const uint16_t ansitab14[16] =
38{
39    0x3800, 0x3805, 0x3850, 0x3855, 0x3d00, 0x3d05, 0x3d28, 0x3d55,
40    0x3aaa, 0x3aaf, 0x3afa, 0x3aff, 0x3faa, 0x3faf, 0x3ffa, 0x3fff,
41};
42
43/** \brief Get the text attribute at the given coordinates.
44 *
45 *  Get the internal \e libcaca attribute value of the character at the
46 *  given coordinates. The attribute value has 32 significant bits,
47 *  organised as follows from MSB to LSB:
48 *  - 3 bits for the background alpha
49 *  - 4 bits for the background red component
50 *  - 4 bits for the background green component
51 *  - 3 bits for the background blue component
52 *  - 3 bits for the foreground alpha
53 *  - 4 bits for the foreground red component
54 *  - 4 bits for the foreground green component
55 *  - 3 bits for the foreground blue component
56 *  - 4 bits for the bold, italics, underline and blink flags
57 *
58 *  If the coordinates are outside the canvas boundaries, the current
59 *  attribute is returned.
60 *
61 *  This function never fails.
62 *
63 *  \param cv A handle to the libcaca canvas.
64 *  \param x X coordinate.
65 *  \param y Y coordinate.
66 *  \return The requested attribute.
67 */
68uint32_t caca_get_attr(caca_canvas_t const *cv, int x, int y)
69{
70    if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
71        return cv->curattr;
72
73    return cv->attrs[x + y * cv->width];
74}
75
76/** \brief Set the default character attribute.
77 *
78 *  Set the default character attribute for drawing. Attributes define
79 *  foreground and background colour, transparency, bold, italics and
80 *  underline styles, as well as blink. String functions such as
81 *  caca_printf() and graphical primitive functions such as caca_draw_line()
82 *  will use this attribute.
83 *
84 *  The value of \e attr is either:
85 *  - a 32-bit integer as returned by caca_get_attr(), in which case it
86 *    also contains colour information,
87 *  - a combination (bitwise OR) of style values (\e CACA_UNDERLINE,
88 *    \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS), in which case
89 *    setting the attribute does not modify the current colour information.
90 *
91 *  To retrieve the current attribute value, use caca_get_attr(-1,-1).
92 *
93 *  This function never fails.
94 *
95 *  \param cv A handle to the libcaca canvas.
96 *  \param attr The requested attribute value.
97 *  \return This function always returns 0.
98 */
99int caca_set_attr(caca_canvas_t *cv, uint32_t attr)
100{
101    if(attr < 0x00000010)
102        attr = (cv->curattr & 0xfffffff0) | attr;
103
104    cv->curattr = attr;
105
106    return 0;
107}
108
109/** \brief Unset flags in the default character attribute.
110 *
111 *  Unset flags in the default character attribute for drawing. Attributes
112 *  define foreground and background colour, transparency, bold, italics and
113 *  underline styles, as well as blink. String functions such as
114 *  caca_printf() and graphical primitive functions such as caca_draw_line()
115 *  will use this attribute.
116 *
117 *  The value of \e attr is a combination (bitwise OR) of style values
118 *  (\e CACA_UNDERLINE, \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS).
119 *  Unsetting these attributes does not modify the current colour information.
120 *
121 *  To retrieve the current attribute value, use caca_get_attr(-1,-1).
122 *
123 *  This function never fails.
124 *
125 *  \param cv A handle to the libcaca canvas.
126 *  \param attr The requested attribute values to unset.
127 *  \return This function always returns 0.
128 */
129int caca_unset_attr(caca_canvas_t *cv, uint32_t attr)
130{
131    cv->curattr &= ~(attr & 0x0000000f);
132
133    return 0;
134}
135
136/** \brief Toggle flags in the default character attribute.
137 *
138 *  Toggle flags in the default character attribute for drawing. Attributes
139 *  define foreground and background colour, transparency, bold, italics and
140 *  underline styles, as well as blink. String functions such as
141 *  caca_printf() and graphical primitive functions such as caca_draw_line()
142 *  will use this attribute.
143 *
144 *  The value of \e attr is a combination (bitwise OR) of style values
145 *  (\e CACA_UNDERLINE, \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS).
146 *  Toggling these attributes does not modify the current colour information.
147 *
148 *  To retrieve the current attribute value, use caca_get_attr(-1,-1).
149 *
150 *  This function never fails.
151 *
152 *  \param cv A handle to the libcaca canvas.
153 *  \param attr The requested attribute values to toggle.
154 *  \return This function always returns 0.
155 */
156int caca_toggle_attr(caca_canvas_t *cv, uint32_t attr)
157{
158    cv->curattr ^= attr & 0x0000000f;
159
160    return 0;
161}
162
163/** \brief Set the character attribute at the given coordinates.
164 *
165 *  Set the character attribute, without changing the character's value. If
166 *  the character at the given coordinates is a fullwidth character, both
167 *  cells' attributes are replaced.
168 *
169 *  The value of \e attr is either:
170 *  - a 32-bit integer as returned by caca_get_attr(), in which case it
171 *    also contains colour information,
172 *  - a combination (bitwise OR) of style values (\e CACA_UNDERLINE,
173 *    \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS), in which case
174 *    setting the attribute does not modify the current colour information.
175 *
176 *  This function never fails.
177 *
178 *  \param cv A handle to the libcaca canvas.
179 *  \param x X coordinate.
180 *  \param y Y coordinate.
181 *  \param attr The requested attribute value.
182 *  \return This function always returns 0.
183 */
184int caca_put_attr(caca_canvas_t *cv, int x, int y, uint32_t attr)
185{
186    uint32_t *curattr, *curchar;
187    int xmin, xmax;
188
189    if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
190        return 0;
191
192    xmin = xmax = x;
193
194    curchar = cv->chars + x + y * cv->width;
195    curattr = cv->attrs + x + y * cv->width;
196
197    if(attr < 0x00000010)
198        curattr[0] = (curattr[0] & 0xfffffff0) | attr;
199    else
200        curattr[0] = attr;
201
202    if(x && curchar[0] == CACA_MAGIC_FULLWIDTH)
203    {
204        curattr[-1] = curattr[0];
205        xmin--;
206    }
207    else if(x + 1 < (int)cv->width && curchar[1] == CACA_MAGIC_FULLWIDTH)
208    {
209        curattr[1] = curattr[0];
210        xmax++;
211    }
212
213    if(!cv->dirty_disabled)
214        caca_add_dirty_rect(cv, xmin, y, xmax - xmin + 1, 1);
215
216    return 0;
217}
218
219/** \brief Set the default colour pair for text (ANSI version).
220 *
221 *  Set the default ANSI colour pair for text drawing. String functions such
222 *  as caca_printf() and graphical primitive functions such as caca_draw_line()
223 *  will use these attributes.
224 *
225 *  Color values are those defined in caca.h, such as CACA_RED
226 *  or CACA_TRANSPARENT.
227 *
228 *  If an error occurs, 0 is returned and \b errno is set accordingly:
229 *  - \c EINVAL At least one of the colour values is invalid.
230 *
231 *  \param cv A handle to the libcaca canvas.
232 *  \param fg The requested ANSI foreground colour.
233 *  \param bg The requested ANSI background colour.
234 *  \return 0 in case of success, -1 if an error occurred.
235 */
236int caca_set_color_ansi(caca_canvas_t *cv, uint8_t fg, uint8_t bg)
237{
238    uint32_t attr;
239
240    if(fg > 0x20 || bg > 0x20)
241    {
242        seterrno(EINVAL);
243        return -1;
244    }
245
246    attr = ((uint32_t)(bg | 0x40) << 18) | ((uint32_t)(fg | 0x40) << 4);
247    cv->curattr = (cv->curattr & 0x0000000f) | attr;
248
249    return 0;
250}
251
252/** \brief Set the default colour pair for text (truecolor version).
253 *
254 *  Set the default ARGB colour pair for text drawing. String functions such
255 *  as caca_printf() and graphical primitive functions such as caca_draw_line()
256 *  will use these attributes.
257 *
258 *  Colors are 16-bit ARGB values, each component being coded on 4 bits. For
259 *  instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
260 *  white with 50% alpha (A=8 R=15 G=15 B=15).
261 *
262 *  This function never fails.
263 *
264 *  \param cv A handle to the libcaca canvas.
265 *  \param fg The requested ARGB foreground colour.
266 *  \param bg The requested ARGB background colour.
267 *  \return This function always returns 0.
268 */
269int caca_set_color_argb(caca_canvas_t *cv, uint16_t fg, uint16_t bg)
270{
271    uint32_t attr;
272
273    if(fg < 0x100)
274        fg += 0x100;
275
276    if(bg < 0x100)
277        bg += 0x100;
278
279    fg = ((fg >> 1) & 0x7ff) | ((fg >> 13) << 11);
280    bg = ((bg >> 1) & 0x7ff) | ((bg >> 13) << 11);
281
282    attr = ((uint32_t)bg << 18) | ((uint32_t)fg << 4);
283    cv->curattr = (cv->curattr & 0x0000000f) | attr;
284
285    return 0;
286}
287
288/** \brief Get DOS ANSI information from attribute.
289 *
290 *  Get the ANSI colour pair for a given attribute. The returned value is
291 *  an 8-bit value whose higher 4 bits are the background colour and lower
292 *  4 bits are the foreground colour.
293 *
294 *  If the attribute has ARGB colours, the nearest colour is used. Special
295 *  attributes such as \e CACA_DEFAULT and \e CACA_TRANSPARENT are not
296 *  handled and are both replaced with \e CACA_LIGHTGRAY for the foreground
297 *  colour and \e CACA_BLACK for the background colour.
298 *
299 *  This function never fails. If the attribute value is outside the expected
300 *  32-bit range, higher order bits are simply ignored.
301 *
302 *  \param attr The requested attribute value.
303 *  \return The corresponding DOS ANSI value.
304 */
305uint8_t caca_attr_to_ansi(uint32_t attr)
306{
307    uint8_t fg = nearest_ansi((attr >> 4) & 0x3fff);
308    uint8_t bg = nearest_ansi(attr >> 18);
309
310    return (fg < 0x10 ? fg : CACA_LIGHTGRAY)
311            | ((bg < 0x10 ? bg : CACA_BLACK) << 4);
312}
313
314/** \brief Get ANSI foreground information from attribute.
315 *
316 *  Get the ANSI foreground colour value for a given attribute. The returned
317 *  value is either one of the \e CACA_RED, \e CACA_BLACK etc. predefined
318 *  colours, or the special value \e CACA_DEFAULT meaning the media's
319 *  default foreground value, or the special value \e CACA_TRANSPARENT.
320 *
321 *  If the attribute has ARGB colours, the nearest colour is returned.
322 *
323 *  This function never fails. If the attribute value is outside the expected
324 *  32-bit range, higher order bits are simply ignored.
325 *
326 *  \param attr The requested attribute value.
327 *  \return The corresponding ANSI foreground value.
328 */
329uint8_t caca_attr_to_ansi_fg(uint32_t attr)
330{
331    return nearest_ansi(((uint16_t)attr >> 4) & 0x3fff);
332}
333
334/** \brief Get ANSI background information from attribute.
335 *
336 *  Get the ANSI background colour value for a given attribute. The returned
337 *  value is either one of the \e CACA_RED, \e CACA_BLACK etc. predefined
338 *  colours, or the special value \e CACA_DEFAULT meaning the media's
339 *  default background value, or the special value \e CACA_TRANSPARENT.
340 *
341 *  If the attribute has ARGB colours, the nearest colour is returned.
342 *
343 *  This function never fails. If the attribute value is outside the expected
344 *  32-bit range, higher order bits are simply ignored.
345 *
346 *  \param attr The requested attribute value.
347 *  \return The corresponding ANSI background value.
348 */
349uint8_t caca_attr_to_ansi_bg(uint32_t attr)
350{
351    return nearest_ansi(attr >> 18);
352}
353
354/** \brief Get 12-bit RGB foreground information from attribute.
355 *
356 *  Get the 12-bit foreground colour value for a given attribute. The returned
357 *  value is a native-endian encoded integer with each red, green and blue
358 *  values encoded on 8 bits in the following order:
359 *   - 8-11 most significant bits: red
360 *   - 4-7 most significant bits: green
361 *   - least significant bits: blue
362 *
363 *  This function never fails. If the attribute value is outside the expected
364 *  32-bit range, higher order bits are simply ignored.
365 *
366 *  \param attr The requested attribute value.
367 *  \return The corresponding 12-bit RGB foreground value.
368 */
369uint16_t caca_attr_to_rgb12_fg(uint32_t attr)
370{
371    uint16_t fg = (attr >> 4) & 0x3fff;
372
373    if(fg < (0x10 | 0x40))
374        return ansitab16[fg ^ 0x40] & 0x0fff;
375
376    if(fg == (CACA_DEFAULT | 0x40))
377        return ansitab16[CACA_LIGHTGRAY] & 0x0fff;
378
379    if(fg == (CACA_TRANSPARENT | 0x40))
380        return ansitab16[CACA_LIGHTGRAY] & 0x0fff;
381
382    return (fg << 1) & 0x0fff;
383}
384
385/** \brief Get 12-bit RGB background information from attribute.
386 *
387 *  Get the 12-bit background colour value for a given attribute. The returned
388 *  value is a native-endian encoded integer with each red, green and blue
389 *  values encoded on 8 bits in the following order:
390 *   - 8-11 most significant bits: red
391 *   - 4-7 most significant bits: green
392 *   - least significant bits: blue
393 *
394 *  This function never fails. If the attribute value is outside the expected
395 *  32-bit range, higher order bits are simply ignored.
396 *
397 *  \param attr The requested attribute value.
398 *  \return The corresponding 12-bit RGB background value.
399 */
400uint16_t caca_attr_to_rgb12_bg(uint32_t attr)
401{
402    uint16_t bg = attr >> 18;
403
404    if(bg < (0x10 | 0x40))
405        return ansitab16[bg ^ 0x40] & 0x0fff;
406
407    if(bg == (CACA_DEFAULT | 0x40))
408        return ansitab16[CACA_BLACK] & 0x0fff;
409
410    if(bg == (CACA_TRANSPARENT | 0x40))
411        return ansitab16[CACA_BLACK] & 0x0fff;
412
413    return (bg << 1) & 0x0fff;
414}
415
416/** \brief Get 64-bit ARGB information from attribute.
417 *
418 *  Get the 64-bit colour and alpha values for a given attribute. The values
419 *  are written as 8-bit integers in the \e argb array in the following order:
420 *   - \e argb[0]: background alpha value
421 *   - \e argb[1]: background red value
422 *   - \e argb[2]: background green value
423 *   - \e argb[3]: background blue value
424 *   - \e argb[4]: foreground alpha value
425 *   - \e argb[5]: foreground red value
426 *   - \e argb[6]: foreground green value
427 *   - \e argb[7]: foreground blue value
428 *
429 *  This function never fails. If the attribute value is outside the expected
430 *  32-bit range, higher order bits are simply ignored.
431 *
432 *  \param attr The requested attribute value.
433 *  \param argb An array of 8-bit integers.
434 */
435void caca_attr_to_argb64(uint32_t attr, uint8_t argb[8])
436{
437    uint16_t fg = (attr >> 4) & 0x3fff;
438    uint16_t bg = attr >> 18;
439
440    if(bg < (0x10 | 0x40))
441        bg = ansitab16[bg ^ 0x40];
442    else if(bg == (CACA_DEFAULT | 0x40))
443        bg = ansitab16[CACA_BLACK];
444    else if(bg == (CACA_TRANSPARENT | 0x40))
445        bg = 0x0fff;
446    else
447        bg = ((bg << 2) & 0xf000) | ((bg << 1) & 0x0fff);
448
449    argb[0] = bg >> 12;
450    argb[1] = (bg >> 8) & 0xf;
451    argb[2] = (bg >> 4) & 0xf;
452    argb[3] = bg & 0xf;
453
454    if(fg < (0x10 | 0x40))
455        fg = ansitab16[fg ^ 0x40];
456    else if(fg == (CACA_DEFAULT | 0x40))
457        fg = ansitab16[CACA_LIGHTGRAY];
458    else if(fg == (CACA_TRANSPARENT | 0x40))
459        fg = 0x0fff;
460    else
461        fg = ((fg << 2) & 0xf000) | ((fg << 1) & 0x0fff);
462
463    argb[4] = fg >> 12;
464    argb[5] = (fg >> 8) & 0xf;
465    argb[6] = (fg >> 4) & 0xf;
466    argb[7] = fg & 0xf;
467}
468
469/*
470 * XXX: the following functions are local
471 */
472
473static uint8_t nearest_ansi(uint16_t argb14)
474{
475    unsigned int i, best, dist;
476
477    if(argb14 < (0x10 | 0x40))
478        return argb14 ^ 0x40;
479
480    if(argb14 == (CACA_DEFAULT | 0x40) || argb14 == (CACA_TRANSPARENT | 0x40))
481        return argb14 ^ 0x40;
482
483    if(argb14 < 0x0fff) /* too transparent */
484        return CACA_TRANSPARENT;
485
486    best = CACA_DEFAULT;
487    dist = 0x3fff;
488    for(i = 0; i < 16; i++)
489    {
490        unsigned int d = 0;
491        int a, b;
492
493        a = (ansitab14[i] >> 7) & 0xf;
494        b = (argb14 >> 7) & 0xf;
495        d += (a - b) * (a - b);
496
497        a = (ansitab14[i] >> 3) & 0xf;
498        b = (argb14 >> 3) & 0xf;
499        d += (a - b) * (a - b);
500
501        a = (ansitab14[i] << 1) & 0xf;
502        b = (argb14 << 1) & 0xf;
503        d += (a - b) * (a - b);
504
505        if(d < dist)
506        {
507            dist = d;
508            best = i;
509        }
510    }
511
512    return best;
513}
514
515#define RGB12TO24(i) \
516   (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
517  | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
518  | ((uint32_t)(i & 0x00f) * 0x000011))
519
520uint32_t _caca_attr_to_rgb24fg(uint32_t attr)
521{
522    return RGB12TO24(caca_attr_to_rgb12_fg(attr));
523}
524
525uint32_t _caca_attr_to_rgb24bg(uint32_t attr)
526{
527    return RGB12TO24(caca_attr_to_rgb12_bg(attr));
528}
529
530/*
531 * XXX: The following functions are aliases.
532 */
533
534uint32_t cucul_get_attr(cucul_canvas_t const *, int, int)
535         CACA_ALIAS(caca_get_attr);
536int cucul_set_attr(cucul_canvas_t *, uint32_t) CACA_ALIAS(caca_set_attr);
537int cucul_put_attr(cucul_canvas_t *, int, int, uint32_t)
538         CACA_ALIAS(caca_put_attr);
539int cucul_set_color_ansi(cucul_canvas_t *, uint8_t, uint8_t)
540         CACA_ALIAS(caca_set_color_ansi);
541int cucul_set_color_argb(cucul_canvas_t *, uint16_t, uint16_t)
542         CACA_ALIAS(caca_set_color_argb);
543uint8_t cucul_attr_to_ansi(uint32_t) CACA_ALIAS(caca_attr_to_ansi);
544uint8_t cucul_attr_to_ansi_fg(uint32_t) CACA_ALIAS(caca_attr_to_ansi_fg);
545uint8_t cucul_attr_to_ansi_bg(uint32_t) CACA_ALIAS(caca_attr_to_ansi_bg);
546uint16_t cucul_attr_to_rgb12_fg(uint32_t) CACA_ALIAS(caca_attr_to_rgb12_fg);
547uint16_t cucul_attr_to_rgb12_bg(uint32_t) CACA_ALIAS(caca_attr_to_rgb12_bg);
548void cucul_attr_to_argb64(uint32_t, uint8_t[8]) CACA_ALIAS(caca_attr_to_argb64);
549
Note: See TracBrowser for help on using the repository browser.