Ignore:
Timestamp:
Nov 25, 2007, 3:12:20 PM (12 years ago)
Author:
Sam Hocevar
Message:
  • Export cucul_attr_to_rgb12_bg(), cucul_attr_to_rgb12_fg() and cucul_attr_to_argb64() (previously _cucul_attr_to_rgb12bg, _cucul_attr_to_rgb12fg and _cucul_attr_to_argb4) in the official libcucul API.
  • Cleanup complete: libcaca no longer depends on "cucul_internals.h".
File:
1 edited

Legend:

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

    r2043 r2057  
    2525
    2626static uint8_t nearest_ansi(uint16_t);
     27
     28/* RGB colours for the ANSI palette. There is no real standard, so we
     29 * use the same values as gnome-terminal. The 7th colour (brown) is a bit
     30 * special: 0xfa50 instead of 0xfaa0. */
     31static const uint16_t ansitab16[16] =
     32{
     33    0xf000, 0xf00a, 0xf0a0, 0xf0aa, 0xfa00, 0xfa0a, 0xfa50, 0xfaaa,
     34    0xf555, 0xf55f, 0xf5f5, 0xf5ff, 0xff55, 0xff5f, 0xfff5, 0xffff,
     35};
     36
     37/* Same table, except on 14 bits (3-4-4-3) */
     38static const uint16_t ansitab14[16] =
     39{
     40    0x3800, 0x3805, 0x3850, 0x3855, 0x3d00, 0x3d05, 0x3d28, 0x3d55,
     41    0x3aaa, 0x3aaf, 0x3afa, 0x3aff, 0x3faa, 0x3faf, 0x3ffa, 0x3fff,
     42};
    2743
    2844/** \brief Get the text attribute at the given coordinates.
     
    292308}
    293309
    294 /*
    295  * XXX: the following functions are local
    296  */
    297 
    298 /* RGB colours for the ANSI palette. There is no real standard, so we
    299  * use the same values as gnome-terminal. The 7th colour (brown) is a bit
    300  * special: 0xfa50 instead of 0xfaa0. */
    301 static const uint16_t ansitab16[16] =
    302 {
    303     0xf000, 0xf00a, 0xf0a0, 0xf0aa, 0xfa00, 0xfa0a, 0xfa50, 0xfaaa,
    304     0xf555, 0xf55f, 0xf5f5, 0xf5ff, 0xff55, 0xff5f, 0xfff5, 0xffff,
    305 };
    306 
    307 /* Same table, except on 14 bits (3-4-4-3) */
    308 static const uint16_t ansitab14[16] =
    309 {
    310     0x3800, 0x3805, 0x3850, 0x3855, 0x3d00, 0x3d05, 0x3d28, 0x3d55,
    311     0x3aaa, 0x3aaf, 0x3afa, 0x3aff, 0x3faa, 0x3faf, 0x3ffa, 0x3fff,
    312 };
    313 
    314 static uint8_t nearest_ansi(uint16_t argb14)
    315 {
    316     unsigned int i, best, dist;
    317 
    318     if(argb14 < (0x10 | 0x40))
    319         return argb14 ^ 0x40;
    320 
    321     if(argb14 == (CUCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))
    322         return argb14 ^ 0x40;
    323 
    324     if(argb14 < 0x0fff) /* too transparent */
    325         return CUCUL_TRANSPARENT;
    326 
    327     best = CUCUL_DEFAULT;
    328     dist = 0x3fff;
    329     for(i = 0; i < 16; i++)
    330     {
    331         unsigned int d = 0;
    332         int a, b;
    333 
    334         a = (ansitab14[i] >> 7) & 0xf;
    335         b = (argb14 >> 7) & 0xf;
    336         d += (a - b) * (a - b);
    337 
    338         a = (ansitab14[i] >> 3) & 0xf;
    339         b = (argb14 >> 3) & 0xf;
    340         d += (a - b) * (a - b);
    341 
    342         a = (ansitab14[i] << 1) & 0xf;
    343         b = (argb14 << 1) & 0xf;
    344         d += (a - b) * (a - b);
    345 
    346         if(d < dist)
    347         {
    348             dist = d;
    349             best = i;
    350         }
    351     }
    352 
    353     return best;
    354 }
    355 
    356 uint16_t _cucul_attr_to_rgb12fg(uint32_t attr)
     310/** \brief Get 12-bit RGB foreground information from attribute.
     311 *
     312 *  Get the 12-bit foreground colour value for a given attribute. The returned
     313 *  value is a native-endian encoded integer with each red, green and blue
     314 *  values encoded on 8 bits in the following order:
     315 *   - 8-11 most significant bits: red
     316 *   - 4-7 most significant bits: green
     317 *   - least significant bits: blue
     318 *
     319 *  This function never fails. If the attribute value is outside the expected
     320 *  32-bit range, higher order bits are simply ignored.
     321 *
     322 *  \param attr The requested attribute value.
     323 *  \return The corresponding 12-bit RGB foreground value.
     324 */
     325unsigned int cucul_attr_to_rgb12_fg(unsigned long int attr)
    357326{
    358327    uint16_t fg = (attr >> 4) & 0x3fff;
     
    370339}
    371340
    372 uint16_t _cucul_attr_to_rgb12bg(uint32_t attr)
     341/** \brief Get 12-bit RGB background information from attribute.
     342 *
     343 *  Get the 12-bit background colour value for a given attribute. The returned
     344 *  value is a native-endian encoded integer with each red, green and blue
     345 *  values encoded on 8 bits in the following order:
     346 *   - 8-11 most significant bits: red
     347 *   - 4-7 most significant bits: green
     348 *   - least significant bits: blue
     349 *
     350 *  This function never fails. If the attribute value is outside the expected
     351 *  32-bit range, higher order bits are simply ignored.
     352 *
     353 *  \param attr The requested attribute value.
     354 *  \return The corresponding 12-bit RGB background value.
     355 */
     356unsigned int cucul_attr_to_rgb12_bg(unsigned long int attr)
    373357{
    374358    uint16_t bg = attr >> 18;
     
    386370}
    387371
    388 #define RGB12TO24(i) \
    389    (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
    390   | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
    391   | ((uint32_t)(i & 0x00f) * 0x000011))
    392 
    393 uint32_t _cucul_attr_to_rgb24fg(uint32_t attr)
    394 {
    395     return RGB12TO24(_cucul_attr_to_rgb12fg(attr));
    396 }
    397 
    398 uint32_t _cucul_attr_to_rgb24bg(uint32_t attr)
    399 {
    400     return RGB12TO24(_cucul_attr_to_rgb12bg(attr));
    401 }
    402 
    403 void _cucul_attr_to_argb4(uint32_t attr, uint8_t argb[8])
     372/** \brief Get 64-bit ARGB information from attribute.
     373 *
     374 *  Get the 64-bit colour and alpha values for a given attribute. The values
     375 *  are written as 8-bit integers in the \e argb array in the following order:
     376 *   - \e argb[0]: background alpha value
     377 *   - \e argb[1]: background red value
     378 *   - \e argb[2]: background green value
     379 *   - \e argb[3]: background blue value
     380 *   - \e argb[4]: foreground alpha value
     381 *   - \e argb[5]: foreground red value
     382 *   - \e argb[6]: foreground green value
     383 *   - \e argb[7]: foreground blue value
     384 *
     385 *  This function never fails. If the attribute value is outside the expected
     386 *  32-bit range, higher order bits are simply ignored.
     387 *
     388 *  \param attr The requested attribute value.
     389 *  \param argb An array of 8-bit integers.
     390 */
     391void cucul_attr_to_argb64(unsigned long int attr, unsigned char argb[8])
    404392{
    405393    uint16_t fg = (attr >> 4) & 0x3fff;
     
    435423}
    436424
     425/*
     426 * XXX: the following functions are local
     427 */
     428
     429static uint8_t nearest_ansi(uint16_t argb14)
     430{
     431    unsigned int i, best, dist;
     432
     433    if(argb14 < (0x10 | 0x40))
     434        return argb14 ^ 0x40;
     435
     436    if(argb14 == (CUCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))
     437        return argb14 ^ 0x40;
     438
     439    if(argb14 < 0x0fff) /* too transparent */
     440        return CUCUL_TRANSPARENT;
     441
     442    best = CUCUL_DEFAULT;
     443    dist = 0x3fff;
     444    for(i = 0; i < 16; i++)
     445    {
     446        unsigned int d = 0;
     447        int a, b;
     448
     449        a = (ansitab14[i] >> 7) & 0xf;
     450        b = (argb14 >> 7) & 0xf;
     451        d += (a - b) * (a - b);
     452
     453        a = (ansitab14[i] >> 3) & 0xf;
     454        b = (argb14 >> 3) & 0xf;
     455        d += (a - b) * (a - b);
     456
     457        a = (ansitab14[i] << 1) & 0xf;
     458        b = (argb14 << 1) & 0xf;
     459        d += (a - b) * (a - b);
     460
     461        if(d < dist)
     462        {
     463            dist = d;
     464            best = i;
     465        }
     466    }
     467
     468    return best;
     469}
     470
     471#define RGB12TO24(i) \
     472   (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
     473  | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
     474  | ((uint32_t)(i & 0x00f) * 0x000011))
     475
     476uint32_t _cucul_attr_to_rgb24fg(uint32_t attr)
     477{
     478    return RGB12TO24(cucul_attr_to_rgb12_fg(attr));
     479}
     480
     481uint32_t _cucul_attr_to_rgb24bg(uint32_t attr)
     482{
     483    return RGB12TO24(cucul_attr_to_rgb12_bg(attr));
     484}
     485
Note: See TracChangeset for help on using the changeset viewer.