diff options
| author | Kyle Gunger <kgunger12@gmail.com> | 2024-05-06 15:28:57 -0400 | 
|---|---|---|
| committer | Kyle Gunger <kgunger12@gmail.com> | 2024-05-06 15:28:57 -0400 | 
| commit | 9cc4f2daad773f27f7f813b4d4b54e248018be0a (patch) | |
| tree | 76b10353308fadd2dfc6f1d1c384f4a571917d08 | |
| parent | 626b54c05ab5a1ddee4dff66d4e2dc9a7053af3e (diff) | |
Color updates
| -rw-r--r-- | include/osm/types.h | 33 | ||||
| -rw-r--r-- | src/types.c | 50 | 
2 files changed, 46 insertions, 37 deletions
diff --git a/include/osm/types.h b/include/osm/types.h index 81a072e..c4393a2 100644 --- a/include/osm/types.h +++ b/include/osm/types.h @@ -1,6 +1,7 @@  #ifndef OSM_TYPES_H  #define OSM_TYPES_H +#include <osm/utils.h>  #include <stdint.h>  #include <stdbool.h> @@ -47,9 +48,14 @@ typedef struct {  } OSMFloatBreakdown;  /// Break a OSMFloat into it's constituant parts -OSMFloatBreakdown osm_break_float(OSMFloat f); +OSMFloatBreakdown osm_float_to_break(OSMFloat f);  /// Break a native float into constituant parts -OSMFloatBreakdown osm_break_native_float(double d); +OSMFloatBreakdown osm_native_float_to_break(double d); + +/// Convert a breakdown to a float +OSMFloat osm_break_to_float(OSMFloatBreakdown b); +/// Convert a breakdown to a native float +double osm_break_to_native_float(OSMFloatBreakdown b);  /// Convert an osm float to a native one  double osm_float_to_native(OSMFloat f); @@ -69,6 +75,11 @@ int8_t osm_is_infinity(OSMFloat f);  // Color +typedef struct { +	uint8_t val; +	uint8_t *name; +} OSMColorChannel; +  /// Color struct with support for extra channels (up to 255)  /// each channel has a range from 0-255  typedef struct { @@ -76,29 +87,21 @@ typedef struct {  		r,  		g,  		b; - -	uint8_t extra; -	uint8_t *ex; -	uint8_t **ex_names; /// Extra channel names encoded in UTF-8 +	Vector +		extra;  } OSMColor; -/// Output rgb from a color struct -uint32_t osm_to_rgb(OSMColor color); -  /// Output color struct from 24-bit RGB  OSMColor osm_rgb_to_color(uint8_t r, uint8_t g, uint8_t b); -/// Parse a 32-bit integer as a 24-bit color. -/// Assumes r, g, and b channels are stores in -/// the lower 24 bits of the number. -OSMColor osm_int_to_color(uint32_t c); -  /// Deep copy a color struct -OSMColor osm_color_copy(const OSMColor *color); +OSMColor osm_color_copy(OSMColor *color);  /// Free a color struct  void osm_color_free(OSMColor *color); +/// Add an extra channel to a color +void osm_add_channel(OSMColor *color, uint8_t val, uint8_t *name);  // Selection diff --git a/src/types.c b/src/types.c index e46a283..2ded2f4 100644 --- a/src/types.c +++ b/src/types.c @@ -64,7 +64,7 @@ OSMFloatBreakdown _osm_ieee754_enlarge(uint64_t d, uint16_t m_len, uint16_t f_le  		// Denormal numbers (TODO)  		out.mantissa = OSM_FLOAT_EXPO_BIAS - bias; -		if (OSM_FLOAT_FRAC_LEN) +		if (OSM_FLOAT_FRAC_LEN >= f_len)  			out.fraction = out.fraction << (OSM_FLOAT_FRAC_LEN - f_len);  // corrected fraction based on difference in bit lengths  		bias = (uint16_t) ceil(log2(out.fraction)); @@ -252,7 +252,7 @@ double _osm_ieee754_assemble(OSMFloatBreakdown b, uint16_t m_len, uint16_t f_len   * WARNING: This code assumes that the compiler   * supports IEEE 754 floating point numbers.   * - * WARNING: This code flushes denormal values to zero + * WARNING: This code flushes subnormal values to zero   *   * WARNING: This code flushes out of bounds values to infinity   * @@ -312,38 +312,35 @@ int8_t osm_is_infinity(OSMFloat f)  	return _osm_is_infinity(osm_float_to_break(f));  } + +/// End of float funcs + +  OSMColor osm_rgb_to_color(uint8_t r, uint8_t g, uint8_t b)  {  	OSMColor out = {  		.r = r,  		.g = g,  		.b = b, -		.extra = 0, -		.ex = NULL, -		.ex_names = NULL +		.extra = vect_init(sizeof(OSMColorChannel)),  	};  	return out;  } -OSMColor osm_color_copy(const OSMColor *color) +OSMColor osm_color_copy(OSMColor *color)  {  	OSMColor out = {  		.r = color->r,  		.g = color->g,  		.b = color->b, -		.extra = color->extra, -		.ex = malloc(color->extra), -		.ex_names = malloc(color->extra * sizeof(uint8_t *)) +		.extra = vect_init(sizeof(OSMColorChannel)),  	}; -	for (uint8_t i = 0; i < color->extra; i++) +	for (uint8_t i = 0; i < color->extra.count; i++)  	{ -		// char should be just one byte long anyways -		size_t len = strlen((char *)color->ex_names[i]) + 1; -		uint8_t *buf = malloc(strlen((char *)color->ex_names[i]) + 1); -		memcpy(buf, color->ex_names[i], len + 1); -		out.ex_names[i] = buf; +		OSMColorChannel *c = vect_get(&color->extra, i); +		osm_add_channel(&out, c->val, c->name);  	}  	return out; @@ -355,16 +352,25 @@ void osm_color_free(OSMColor *color)  	color->g = 0;  	color->b = 0; -	for (uint8_t i = 0; i < color->extra; i++) +	for (uint8_t i = 0; i < color->extra.count; i++)  	{ -		free(color->ex_names[i]); +		OSMColorChannel *c = vect_get(&color->extra, i); +		free(c->name);  	} -	free(color->ex_names); -	free(color->ex); +	vect_end(&color->extra); +} + +void osm_add_channel(OSMColor *color, uint8_t val, uint8_t *name) +{ +	size_t len = strlen((char *)name) + 1; +	OSMColorChannel add = { +		.val = val, +		.name = malloc(len) +	}; -	color->ex_names = NULL; -	color->ex = NULL; +	memcpy(add.name, name, len); -	color->extra = 0; +	vect_push(&color->extra, &add);  } +  |