Skip to content

Commit

Permalink
remove get_hsv_cost in favor of pbio_get_cone_cost
Browse files Browse the repository at this point in the history
  • Loading branch information
Novakasa committed Apr 11, 2022
1 parent c8b857e commit 5759b70
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/pbio/src/color/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <pbio/color.h>

// Cost function between two colors a and b. The lower, the closer they are.
int32_t pbio_get_hsv_cost(const pbio_color_hsv_t *x, const pbio_color_hsv_t *c) {

// Calculate the hue error
Expand Down Expand Up @@ -32,7 +33,8 @@ int32_t pbio_get_hsv_cost(const pbio_color_hsv_t *x, const pbio_color_hsv_t *c)
return hue_error * hue_error + 5 * saturation_error * saturation_error + 2 * value_error * value_error;
}

int32_t pbio_get_cone_cost(const pbio_color_hsv_t *hsv_a, const pbio_color_hsv_t *hsv_b) {
// gets squared cartesian distance between hsv colors mapped into a chroma-value-cone
int32_t pbio_get_cone_cost(const pbio_color_hsv_t *hsv_a, const pbio_color_hsv_t *hsv_b, const int32_t chroma_weight) {

pbio_color_hsv_fix16_t a, b;
pbio_color_hsv_to_fix16(hsv_a, &a);
Expand Down
7 changes: 1 addition & 6 deletions pybricks/util_pb/pb_color_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ void pb_color_map_save_default(mp_obj_t *color_map) {
*color_map = MP_OBJ_FROM_PTR(&pb_color_map_default);
}

// Cost function between two colors a and b. The lower, the closer they are.
static int32_t get_hsv_cost(const pbio_color_hsv_t *a, const pbio_color_hsv_t *b) {
return pbio_get_cone_cost(a, b);
}

// Get a discrete color that matches the given hsv values most closely
mp_obj_t pb_color_map_get_color(mp_obj_t *color_map, pbio_color_hsv_t *hsv) {

Expand All @@ -90,7 +85,7 @@ mp_obj_t pb_color_map_get_color(mp_obj_t *color_map, pbio_color_hsv_t *hsv) {
for (size_t i = 0; i < n; i++) {

// Evaluate the cost function
cost_now = get_hsv_cost(hsv, pb_type_Color_get_hsv(colors[i]));
cost_now = pbio_get_hsv_cost(hsv, pb_type_Color_get_hsv(colors[i]));

// If cost is less than before, update the minimum and the match
if (cost_now < cost_min) {
Expand Down

0 comments on commit 5759b70

Please sign in to comment.