add simple color data types

Mon, 30 Oct 2023 18:42:09 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 30 Oct 2023 18:42:09 +0100
changeset 4
b7acda6a4476
parent 3
1efd6da2ad53
child 5
7e1196d551ff

add simple color data types

src/ascension/datatypes.h file | annotate | diff | comparison | revisions
     1.1 --- a/src/ascension/datatypes.h	Mon Oct 30 18:09:27 2023 +0100
     1.2 +++ b/src/ascension/datatypes.h	Mon Oct 30 18:42:09 2023 +0100
     1.3 @@ -28,16 +28,66 @@
     1.4  #ifndef ASCENSION_DATATYPES_H
     1.5  #define ASCENSION_DATATYPES_H
     1.6  
     1.7 +#include <SDL2/SDL_endian.h>
     1.8 +
     1.9  #ifdef __cplusplus
    1.10  extern "C" {
    1.11  #endif
    1.12  
    1.13 +typedef unsigned char asc_ubyte;
    1.14 +typedef signed char asc_sbyte;
    1.15 +
    1.16  typedef union {
    1.17      int data[2];
    1.18      struct { int x, y; };
    1.19      struct { int width, height; };
    1.20  } asc_vec2i;
    1.21  
    1.22 +typedef union {
    1.23 +    asc_ubyte data[4];
    1.24 +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    1.25 +    struct { asc_ubyte red, green, blue, alpha; };
    1.26 +#else
    1.27 +    struct { asc_ubyte alpha, blue, green, red; };
    1.28 +#endif
    1.29 +} asc_col4i;
    1.30 +
    1.31 +typedef union {
    1.32 +    float data[4];
    1.33 +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    1.34 +    struct { float red, green, blue, alpha; };
    1.35 +#else
    1.36 +    struct { float alpha, blue, green, red; };
    1.37 +#endif
    1.38 +} asc_col4f;
    1.39 +
    1.40 +static inline int asc_clamp_i(int v, int min, int max) {
    1.41 +    if (v < min) return min;
    1.42 +    if (v > max) return max;
    1.43 +    return v;
    1.44 +}
    1.45 +
    1.46 +/**
    1.47 + * Converts a float color (0.0f to 1.0f) to an int color (0 to 255).
    1.48 + *
    1.49 + * This operation is quite expensive. When you need to use it often, it's
    1.50 + * quite obvious that you should change the data type.
    1.51 + *
    1.52 + * @param c the color using floats
    1.53 + * @return the same color using ints
    1.54 + */
    1.55 +static inline asc_col4i asc_col_ftoi(asc_col4f c) {
    1.56 +    int red = (int)(255*c.red);
    1.57 +    int green = (int)(255*c.green);
    1.58 +    int blue = (int)(255*c.blue);
    1.59 +    int alpha = (int)(255*c.alpha);
    1.60 +    asc_col4i r;
    1.61 +    r.red = (asc_ubyte)asc_clamp_i(red, 0, 255);
    1.62 +    r.green = (asc_ubyte)asc_clamp_i(green, 0, 255);
    1.63 +    r.blue = (asc_ubyte)asc_clamp_i(blue, 0, 255);
    1.64 +    r.alpha = (asc_ubyte)asc_clamp_i(alpha, 0, 255);
    1.65 +    return r;
    1.66 +}
    1.67  
    1.68  #ifdef __cplusplus
    1.69  } // extern "C"

mercurial