RGB color via localevent

Solves #1963
Solves #1991
This commit is contained in:
Edward Firmo
2024-03-27 14:24:39 +01:00
parent 04ae094baf
commit c2ad0e9669
3 changed files with 117 additions and 28 deletions

View File

@@ -46,6 +46,20 @@ namespace nspanel_ha_blueprint {
return false;
}
/**
* Retrieves a string value from a cJSON item or returns a default value if the item is not a string.
*
* @param json_item The cJSON item from which to retrieve the string value.
* @param default_value The default value to return if the cJSON item is not a string. Defaults to "".
* @return The string value of the cJSON item, or the default value if the item is not a string.
*/
const char* json_get_text(const cJSON* json_item, const char* default_value = "") {
if (json_item != NULL && cJSON_IsString(json_item) && json_item->valuestring != NULL) {
return json_item->valuestring;
}
return default_value;
}
/**
* Retrieves a floating-point value from a cJSON object.
*
@@ -59,6 +73,51 @@ namespace nspanel_ha_blueprint {
return std::nan(""); // Return NaN if not a valid number
}
/**
* Extracts RGB color values from a cJSON array.
*
* This function is designed to extract an array of integers from a given cJSON array object,
* specifically targeting RGB color values. It expects the array to contain exactly three integers,
* each representing the red, green, and blue components of a color, in that order.
*
* @param value The cJSON array object from which to extract the RGB values. This object
* should be an array containing exactly three elements, each of which should
* be an integer between 0 and 255.
* @return A dynamically allocated array of three integers, representing the RGB color
* values extracted from the JSON array. The caller is responsible for freeing
* this memory using `free()` once the array is no longer needed. If the input
* does not meet the expected format (not an array, not exactly three elements,
* or contains non-integer values), the function returns NULL.
*
* Example JSON input: {"page": "light", "component": "rgb_color", "value": [127, 255, 159]}
* In this example, the function would return an int array containing {127, 255, 159}.
*
* Note: This function performs dynamic memory allocation for the returned array.
* It's crucial to free the allocated memory using `free()` when done with the array
* to prevent memory leaks.
*/
int* json_extract_rgb_array(const cJSON* value) {
if (!cJSON_IsArray(value) || cJSON_GetArraySize(value) != 3) {
return NULL; // Ensures the array exists and contains exactly 3 elements
}
int* rgb = (int*)malloc(3 * sizeof(int)); // Allocate space for 3 integers
if (!rgb) {
return NULL; // Allocation failed
}
for (int i = 0; i < 3; ++i) {
cJSON* item = cJSON_GetArrayItem(value, i);
if (!cJSON_IsNumber(item)) {
free(rgb);
return NULL; // Ensures all elements are numbers
}
rgb[i] = item->valueint;
}
return rgb;
}
/**
* Converts a JSON integer percentage value to a string representing a floating-point number between 0.00 and 1.00.
*