Ignore:
Timestamp:
Sep 27, 2008, 3:12:46 PM (11 years ago)
Author:
Sam Hocevar
Message:

Starting refactoring to get rid of libcucul. The initial reason for the
split is rendered moot by the plugin system: when enabled, binaries do
not link directly with libX11 or libGL. I hope this is a step towards
more consisteny and clarity.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/attr.c

    r2819 r2821  
    11/*
    2  *  libcucul      Canvas for ultrafast compositing of Unicode letters
     2 *  libcaca       Colour ASCII-Art library
    33 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
    44 *                All Rights Reserved
     
    2020#include "config.h"
    2121
    22 #include "cucul.h"
    23 #include "cucul_internals.h"
     22#include "caca.h"
     23#include "caca_internals.h"
    2424
    2525static uint8_t nearest_ansi(uint16_t);
     
    4343/** \brief Get the text attribute at the given coordinates.
    4444 *
    45  *  Get the internal \e libcucul attribute value of the character at the
     45 *  Get the internal \e libcaca attribute value of the character at the
    4646 *  given coordinates. The attribute value has 32 significant bits,
    4747 *  organised as follows from MSB to LSB:
     
    6161 *  This function never fails.
    6262 *
    63  *  \param cv A handle to the libcucul canvas.
     63 *  \param cv A handle to the libcaca canvas.
    6464 *  \param x X coordinate.
    6565 *  \param y Y coordinate.
    6666 *  \return The requested attribute.
    6767 */
    68 uint32_t cucul_get_attr(cucul_canvas_t const *cv, int x, int y)
     68uint32_t caca_get_attr(caca_canvas_t const *cv, int x, int y)
    6969{
    7070    if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
     
    8383 *
    8484 *  The value of \e attr is either:
    85  *  - a 32-bit integer as returned by cucul_get_attr(), in which case it
     85 *  - a 32-bit integer as returned by caca_get_attr(), in which case it
    8686 *    also contains colour information,
    87  *  - a combination (bitwise OR) of style values (\e CUCUL_UNDERLINE,
    88  *    \e CUCUL_BLINK, \e CUCUL_BOLD and \e CUCUL_ITALICS), in which case
     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
    8989 *    setting the attribute does not modify the current colour information.
    9090 *
    91  *  To retrieve the current attribute value, use cucul_get_attr(-1,-1).
     91 *  To retrieve the current attribute value, use caca_get_attr(-1,-1).
    9292 *
    9393 *  This function never fails.
    9494 *
    95  *  \param cv A handle to the libcucul canvas.
     95 *  \param cv A handle to the libcaca canvas.
    9696 *  \param attr The requested attribute value.
    9797 *  \return This function always returns 0.
    9898 */
    99 int cucul_set_attr(cucul_canvas_t *cv, uint32_t attr)
     99int caca_set_attr(caca_canvas_t *cv, uint32_t attr)
    100100{
    101101    if(attr < 0x00000010)
     
    114114 *
    115115 *  The value of \e attr is either:
    116  *  - a 32-bit integer as returned by cucul_get_attr(), in which case it
     116 *  - a 32-bit integer as returned by caca_get_attr(), in which case it
    117117 *    also contains colour information,
    118  *  - a combination (bitwise OR) of style values (\e CUCUL_UNDERLINE,
    119  *    \e CUCUL_BLINK, \e CUCUL_BOLD and \e CUCUL_ITALICS), in which case
     118 *  - a combination (bitwise OR) of style values (\e CACA_UNDERLINE,
     119 *    \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS), in which case
    120120 *    setting the attribute does not modify the current colour information.
    121121 *
    122122 *  This function never fails.
    123123 *
    124  *  \param cv A handle to the libcucul canvas.
     124 *  \param cv A handle to the libcaca canvas.
    125125 *  \param x X coordinate.
    126126 *  \param y Y coordinate.
     
    128128 *  \return This function always returns 0.
    129129 */
    130 int cucul_put_attr(cucul_canvas_t *cv, int x, int y, uint32_t attr)
     130int caca_put_attr(caca_canvas_t *cv, int x, int y, uint32_t attr)
    131131{
    132132    uint32_t *curattr, *curchar;
     
    143143        curattr[0] = attr;
    144144
    145     if(x && curchar[0] == CUCUL_MAGIC_FULLWIDTH)
     145    if(x && curchar[0] == CACA_MAGIC_FULLWIDTH)
    146146        curattr[-1] = curattr[0];
    147     else if(x + 1 < (int)cv->width && curchar[1] == CUCUL_MAGIC_FULLWIDTH)
     147    else if(x + 1 < (int)cv->width && curchar[1] == CACA_MAGIC_FULLWIDTH)
    148148        curattr[1] = curattr[0];
    149149
     
    157157 *  will use these attributes.
    158158 *
    159  *  Color values are those defined in cucul.h, such as CUCUL_RED
    160  *  or CUCUL_TRANSPARENT.
     159 *  Color values are those defined in caca.h, such as CACA_RED
     160 *  or CACA_TRANSPARENT.
    161161 *
    162162 *  If an error occurs, 0 is returned and \b errno is set accordingly:
    163163 *  - \c EINVAL At least one of the colour values is invalid.
    164164 *
    165  *  \param cv A handle to the libcucul canvas.
     165 *  \param cv A handle to the libcaca canvas.
    166166 *  \param fg The requested ANSI foreground colour.
    167167 *  \param bg The requested ANSI background colour.
    168168 *  \return 0 in case of success, -1 if an error occurred.
    169169 */
    170 int cucul_set_color_ansi(cucul_canvas_t *cv, uint8_t fg, uint8_t bg)
     170int caca_set_color_ansi(caca_canvas_t *cv, uint8_t fg, uint8_t bg)
    171171{
    172172    uint32_t attr;
     
    196196 *  This function never fails.
    197197 *
    198  *  \param cv A handle to the libcucul canvas.
     198 *  \param cv A handle to the libcaca canvas.
    199199 *  \param fg The requested ARGB foreground colour.
    200200 *  \param bg The requested ARGB background colour.
    201201 *  \return This function always returns 0.
    202202 */
    203 int cucul_set_color_argb(cucul_canvas_t *cv, uint16_t fg, uint16_t bg)
     203int caca_set_color_argb(caca_canvas_t *cv, uint16_t fg, uint16_t bg)
    204204{
    205205    uint32_t attr;
     
    227227 *
    228228 *  If the attribute has ARGB colours, the nearest colour is used. Special
    229  *  attributes such as \e CUCUL_DEFAULT and \e CUCUL_TRANSPARENT are not
    230  *  handled and are both replaced with \e CUCUL_LIGHTGRAY for the foreground
    231  *  colour and \e CUCUL_BLACK for the background colour.
     229 *  attributes such as \e CACA_DEFAULT and \e CACA_TRANSPARENT are not
     230 *  handled and are both replaced with \e CACA_LIGHTGRAY for the foreground
     231 *  colour and \e CACA_BLACK for the background colour.
    232232 *
    233233 *  This function never fails. If the attribute value is outside the expected
     
    237237 *  \return The corresponding DOS ANSI value.
    238238 */
    239 uint8_t cucul_attr_to_ansi(uint32_t attr)
     239uint8_t caca_attr_to_ansi(uint32_t attr)
    240240{
    241241    uint8_t fg = nearest_ansi((attr >> 4) & 0x3fff);
    242242    uint8_t bg = nearest_ansi(attr >> 18);
    243243
    244     return (fg < 0x10 ? fg : CUCUL_LIGHTGRAY)
    245             | ((bg < 0x10 ? bg : CUCUL_BLACK) << 4);
     244    return (fg < 0x10 ? fg : CACA_LIGHTGRAY)
     245            | ((bg < 0x10 ? bg : CACA_BLACK) << 4);
    246246}
    247247
     
    249249 *
    250250 *  Get the ANSI foreground colour value for a given attribute. The returned
    251  *  value is either one of the \e CUCUL_RED, \e CUCUL_BLACK etc. predefined
    252  *  colours, or the special value \e CUCUL_DEFAULT meaning the media's
    253  *  default foreground value, or the special value \e CUCUL_TRANSPARENT.
     251 *  value is either one of the \e CACA_RED, \e CACA_BLACK etc. predefined
     252 *  colours, or the special value \e CACA_DEFAULT meaning the media's
     253 *  default foreground value, or the special value \e CACA_TRANSPARENT.
    254254 *
    255255 *  If the attribute has ARGB colours, the nearest colour is returned.
     
    261261 *  \return The corresponding ANSI foreground value.
    262262 */
    263 uint8_t cucul_attr_to_ansi_fg(uint32_t attr)
     263uint8_t caca_attr_to_ansi_fg(uint32_t attr)
    264264{
    265265    return nearest_ansi(((uint16_t)attr >> 4) & 0x3fff);
     
    269269 *
    270270 *  Get the ANSI background colour value for a given attribute. The returned
    271  *  value is either one of the \e CUCUL_RED, \e CUCUL_BLACK etc. predefined
    272  *  colours, or the special value \e CUCUL_DEFAULT meaning the media's
    273  *  default background value, or the special value \e CUCUL_TRANSPARENT.
     271 *  value is either one of the \e CACA_RED, \e CACA_BLACK etc. predefined
     272 *  colours, or the special value \e CACA_DEFAULT meaning the media's
     273 *  default background value, or the special value \e CACA_TRANSPARENT.
    274274 *
    275275 *  If the attribute has ARGB colours, the nearest colour is returned.
     
    281281 *  \return The corresponding ANSI background value.
    282282 */
    283 uint8_t cucul_attr_to_ansi_bg(uint32_t attr)
     283uint8_t caca_attr_to_ansi_bg(uint32_t attr)
    284284{
    285285    return nearest_ansi(attr >> 18);
     
    301301 *  \return The corresponding 12-bit RGB foreground value.
    302302 */
    303 uint16_t cucul_attr_to_rgb12_fg(uint32_t attr)
     303uint16_t caca_attr_to_rgb12_fg(uint32_t attr)
    304304{
    305305    uint16_t fg = (attr >> 4) & 0x3fff;
     
    308308        return ansitab16[fg ^ 0x40] & 0x0fff;
    309309
    310     if(fg == (CUCUL_DEFAULT | 0x40))
    311         return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
    312 
    313     if(fg == (CUCUL_TRANSPARENT | 0x40))
    314         return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
     310    if(fg == (CACA_DEFAULT | 0x40))
     311        return ansitab16[CACA_LIGHTGRAY] & 0x0fff;
     312
     313    if(fg == (CACA_TRANSPARENT | 0x40))
     314        return ansitab16[CACA_LIGHTGRAY] & 0x0fff;
    315315
    316316    return (fg << 1) & 0x0fff;
     
    332332 *  \return The corresponding 12-bit RGB background value.
    333333 */
    334 uint16_t cucul_attr_to_rgb12_bg(uint32_t attr)
     334uint16_t caca_attr_to_rgb12_bg(uint32_t attr)
    335335{
    336336    uint16_t bg = attr >> 18;
     
    339339        return ansitab16[bg ^ 0x40] & 0x0fff;
    340340
    341     if(bg == (CUCUL_DEFAULT | 0x40))
    342         return ansitab16[CUCUL_BLACK] & 0x0fff;
    343 
    344     if(bg == (CUCUL_TRANSPARENT | 0x40))
    345         return ansitab16[CUCUL_BLACK] & 0x0fff;
     341    if(bg == (CACA_DEFAULT | 0x40))
     342        return ansitab16[CACA_BLACK] & 0x0fff;
     343
     344    if(bg == (CACA_TRANSPARENT | 0x40))
     345        return ansitab16[CACA_BLACK] & 0x0fff;
    346346
    347347    return (bg << 1) & 0x0fff;
     
    367367 *  \param argb An array of 8-bit integers.
    368368 */
    369 void cucul_attr_to_argb64(uint32_t attr, uint8_t argb[8])
     369void caca_attr_to_argb64(uint32_t attr, uint8_t argb[8])
    370370{
    371371    uint16_t fg = (attr >> 4) & 0x3fff;
     
    374374    if(bg < (0x10 | 0x40))
    375375        bg = ansitab16[bg ^ 0x40];
    376     else if(bg == (CUCUL_DEFAULT | 0x40))
    377         bg = ansitab16[CUCUL_BLACK];
    378     else if(bg == (CUCUL_TRANSPARENT | 0x40))
     376    else if(bg == (CACA_DEFAULT | 0x40))
     377        bg = ansitab16[CACA_BLACK];
     378    else if(bg == (CACA_TRANSPARENT | 0x40))
    379379        bg = 0x0fff;
    380380    else
     
    388388    if(fg < (0x10 | 0x40))
    389389        fg = ansitab16[fg ^ 0x40];
    390     else if(fg == (CUCUL_DEFAULT | 0x40))
    391         fg = ansitab16[CUCUL_LIGHTGRAY];
    392     else if(fg == (CUCUL_TRANSPARENT | 0x40))
     390    else if(fg == (CACA_DEFAULT | 0x40))
     391        fg = ansitab16[CACA_LIGHTGRAY];
     392    else if(fg == (CACA_TRANSPARENT | 0x40))
    393393        fg = 0x0fff;
    394394    else
     
    412412        return argb14 ^ 0x40;
    413413
    414     if(argb14 == (CUCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))
     414    if(argb14 == (CACA_DEFAULT | 0x40) || argb14 == (CACA_TRANSPARENT | 0x40))
    415415        return argb14 ^ 0x40;
    416416
    417417    if(argb14 < 0x0fff) /* too transparent */
    418         return CUCUL_TRANSPARENT;
    419 
    420     best = CUCUL_DEFAULT;
     418        return CACA_TRANSPARENT;
     419
     420    best = CACA_DEFAULT;
    421421    dist = 0x3fff;
    422422    for(i = 0; i < 16; i++)
     
    452452  | ((uint32_t)(i & 0x00f) * 0x000011))
    453453
    454 uint32_t _cucul_attr_to_rgb24fg(uint32_t attr)
    455 {
    456     return RGB12TO24(cucul_attr_to_rgb12_fg(attr));
    457 }
    458 
    459 uint32_t _cucul_attr_to_rgb24bg(uint32_t attr)
    460 {
    461     return RGB12TO24(cucul_attr_to_rgb12_bg(attr));
    462 }
    463 
     454uint32_t _caca_attr_to_rgb24fg(uint32_t attr)
     455{
     456    return RGB12TO24(caca_attr_to_rgb12_fg(attr));
     457}
     458
     459uint32_t _caca_attr_to_rgb24bg(uint32_t attr)
     460{
     461    return RGB12TO24(caca_attr_to_rgb12_bg(attr));
     462}
     463
Note: See TracChangeset for help on using the changeset viewer.