diff --git a/particles/ramp.c b/particles/ramp.c index c8c2fd5..31df037 100644 --- a/particles/ramp.c +++ b/particles/ramp.c @@ -33,13 +33,21 @@ instantiate(const struct particle *particle, const struct tag_set *tags) const struct tag *tag = tag_for_name(tags, ramp->tag); assert(tag != NULL); + assert(ramp->count > 0); + long value = tag->as_int(tag); long min = tag->min(tag); long max = tag->max(tag); - double progress = (double)value / (max - min); + assert(value >= min && value <= max); + assert(max >= min); - size_t idx = progress * ramp->count; + size_t idx = 0; + if (max - min > 0) + idx = ramp->count * value / (max - min); + + if (idx == ramp->count) + idx--; /* * printf("ramp: value: %lu, min: %lu, max: %lu, progress: %f, idx: %zu\n", * value, min, max, progress, idx);