Changeset 1331 for libcaca


Ignore:
Timestamp:
Nov 11, 2006, 9:34:41 AM (16 years ago)
Author:
Sam Hocevar
Message:
  • Export cucul_attr_to_ansi_fg() and cucul_attr_to_ansi_bg().
Location:
libcaca/trunk/cucul
Files:
2 edited

Legend:

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

    r1324 r1331  
    2626#include "cucul.h"
    2727#include "cucul_internals.h"
     28
     29static uint8_t nearest_ansi(uint16_t);
    2830
    2931/** \brief Get the text attribute at the given coordinates.
     
    233235}
    234236
     237/** \brief Get ANSI foreground information from attribute.
     238 *
     239 *  Get the ANSI foreground colour value for a given attribute. The returned
     240 *  value is either one of the \e CUCUL_RED, \e CUCUL_BLACK etc. predefined
     241 *  colours, or the special value \e CUCUL_DEFAULT meaning the media's
     242 *  default foreground value, or the special value \e CUCUL_TRANSPARENT.
     243 *
     244 *  If the attribute has ARGB colours, the nearest colour is returned.
     245 *
     246 *  This function never fails. If the attribute value is outside the expected
     247 *  32-bit range, higher order bits are simply ignored.
     248 *
     249 *  \param attr The requested attribute value.
     250 *  \return The corresponding ANSI foreground value.
     251 */
     252unsigned char cucul_attr_to_ansi_fg(unsigned long int attr)
     253{
     254    return nearest_ansi(((uint16_t)attr >> 4) & 0x3fff);
     255}
     256
     257/** \brief Get ANSI background information from attribute.
     258 *
     259 *  Get the ANSI background colour value for a given attribute. The returned
     260 *  value is either one of the \e CUCUL_RED, \e CUCUL_BLACK etc. predefined
     261 *  colours, or the special value \e CUCUL_DEFAULT meaning the media's
     262 *  default background value, or the special value \e CUCUL_TRANSPARENT.
     263 *
     264 *  If the attribute has ARGB colours, the nearest colour is returned.
     265 *
     266 *  This function never fails. If the attribute value is outside the expected
     267 *  32-bit range, higher order bits are simply ignored.
     268 *
     269 *  \param attr The requested attribute value.
     270 *  \return The corresponding ANSI background value.
     271 */
     272unsigned char cucul_attr_to_ansi_bg(unsigned long int attr)
     273{
     274    return nearest_ansi(attr >> 18);
     275}
     276
    235277/*
    236278 * XXX: the following functions are local
     
    253295};
    254296
    255 static uint8_t nearest_ansi(uint16_t argb14, uint8_t def)
     297static uint8_t nearest_ansi(uint16_t argb14)
    256298{
    257299    unsigned int i, best, dist;
    258300
    259     if(argb14 < 0x0050)
     301    if(argb14 < (0x10 | 0x40))
    260302        return argb14 ^ 0x40;
    261303
    262304    if(argb14 == (CUCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))
    263         return def;
    264 
    265     if(argb14 < 0x0fff) /* too transparent, return default colour */
    266         return def;
    267 
    268     best = def;
     305        return argb14 ^ 0x40;
     306
     307    if(argb14 < 0x0fff) /* too transparent */
     308        return CUCUL_TRANSPARENT;
     309
     310    best = CUCUL_DEFAULT;
    269311    dist = 0x3fff;
    270312    for(i = 0; i < 16; i++)
     
    297339uint8_t _cucul_attr_to_ansi8(uint32_t attr)
    298340{
     341    uint8_t fg = nearest_ansi((attr >> 4) & 0x3fff);
     342    uint8_t bg = nearest_ansi(attr >> 18);
     343
     344    if(fg == CUCUL_DEFAULT || fg == CUCUL_TRANSPARENT)
     345        fg = CUCUL_LIGHTGRAY;
     346
     347    if(bg == CUCUL_DEFAULT || bg == CUCUL_TRANSPARENT)
     348        bg = CUCUL_BLACK;
     349
     350    return fg | (bg << 4);
     351}
     352
     353uint8_t _cucul_attr_to_ansi4fg(uint32_t attr)
     354{
     355    uint8_t c = nearest_ansi((attr >> 4) & 0x3fff);
     356
     357    if(c == CUCUL_DEFAULT || c == CUCUL_TRANSPARENT)
     358        return CUCUL_LIGHTGRAY;
     359
     360    return c;
     361}
     362
     363uint8_t _cucul_attr_to_ansi4bg(uint32_t attr)
     364{
     365    uint8_t c = nearest_ansi(attr >> 18);
     366
     367    if(c == CUCUL_DEFAULT || c == CUCUL_TRANSPARENT)
     368        return CUCUL_BLACK;
     369
     370    return c;
     371}
     372
     373uint16_t _cucul_attr_to_rgb12fg(uint32_t attr)
     374{
    299375    uint16_t fg = (attr >> 4) & 0x3fff;
    300     uint16_t bg = attr >> 18;
    301 
    302     return nearest_ansi(fg, CUCUL_LIGHTGRAY)
    303             | (nearest_ansi(bg, CUCUL_BLACK) << 4);
    304 }
    305 
    306 uint8_t _cucul_attr_to_ansi4fg(uint32_t attr)
    307 {
    308     return nearest_ansi((attr >> 4) & 0x3fff, CUCUL_LIGHTGRAY);
    309 }
    310 
    311 uint8_t _cucul_attr_to_ansi4bg(uint32_t attr)
    312 {
    313     return nearest_ansi(attr >> 18, CUCUL_BLACK);
    314 }
    315 
    316 uint16_t _cucul_attr_to_rgb12fg(uint32_t attr)
    317 {
    318     uint16_t fg = (attr >> 4) & 0x3fff;
    319 
    320     if(fg < 0x0050)
     376
     377    if(fg < (0x10 | 0x40))
    321378        return ansitab16[fg ^ 0x40] & 0x0fff;
    322379
     
    334391    uint16_t bg = attr >> 18;
    335392
    336     if(bg < 0x0050)
     393    if(bg < (0x10 | 0x40))
    337394        return ansitab16[bg ^ 0x40] & 0x0fff;
    338395
     
    366423    uint16_t bg = attr >> 18;
    367424
    368     if(bg < 0x0050)
     425    if(bg < (0x10 | 0x40))
    369426        bg = ansitab16[bg ^ 0x40];
    370427    else if(bg == (CUCUL_DEFAULT | 0x40))
     
    380437    argb[3] = bg & 0xf;
    381438
    382     if(fg < 0x0050)
     439    if(fg < (0x10 | 0x40))
    383440        fg = ansitab16[fg ^ 0x40];
    384441    else if(fg == (CUCUL_DEFAULT | 0x40))
  • libcaca/trunk/cucul/cucul.h

    r1330 r1331  
    117117/*  @} */
    118118
     119/** \defgroup attributes libcucul attribute conversions
     120 *
     121 *  These functions perform conversions between attribute values.
     122 *
     123 *  @{ */
     124unsigned char cucul_attr_to_ansi_fg(unsigned long int);
     125unsigned char cucul_attr_to_ansi_bg(unsigned long int);
     126/*  @} */
     127
    119128/** \defgroup charset libcucul character set conversions
    120129 *
     
    122131 *
    123132 *  @{ */
    124 extern unsigned long int cucul_utf8_to_utf32(char const *, unsigned int *);
    125 extern unsigned int cucul_utf32_to_utf8(char *, unsigned long int);
    126 extern unsigned char cucul_utf32_to_cp437(unsigned long int);
    127 extern unsigned long int cucul_cp437_to_utf32(unsigned char);
    128 extern int cucul_utf32_is_fullwidth(unsigned long int);
     133unsigned long int cucul_utf8_to_utf32(char const *, unsigned int *);
     134unsigned int cucul_utf32_to_utf8(char *, unsigned long int);
     135unsigned char cucul_utf32_to_cp437(unsigned long int);
     136unsigned long int cucul_cp437_to_utf32(unsigned char);
     137int cucul_utf32_is_fullwidth(unsigned long int);
    129138/*  @} */
    130139
Note: See TracChangeset for help on using the changeset viewer.