Commit 8e29f209 authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

moving hsla color computation into the model

parent 356c29bc
......@@ -59,6 +59,33 @@ class GlobalViralHostResponseValue(models.Model):
)
return h * 360, s * 100, v * 50
@classmethod
def get_hsv_color_for(cls, value):
lower = cls.objects_mappable().filter(value__lte=value).order_by("-value").first()
upper = cls.objects_mappable().filter(value__gte=value).order_by("value").first()
lower_hsv = lower.to_hsv()
upper_hsv = upper.to_hsv()
if lower.value == upper.value:
percentage = 0
else:
percentage = (value - lower.value) / (upper.value - lower.value)
hue = lower_hsv[0] * percentage + upper_hsv[0] * (1 - percentage)
sat = lower_hsv[1] * percentage + upper_hsv[1] * (1 - percentage)
lgt = lower_hsv[2] * percentage + upper_hsv[2] * (1 - percentage)
return hue, sat, lgt
@classmethod
def get_rgb_color_for(cls, value):
hue, sat, lgt = cls.get_hsv_color_for(value=value)
r, g, b = colorsys.hsv_to_rgb(hue / 360, sat / 100, lgt / 50)
return int(r * 255), int(g * 255), int(b * 255)
@classmethod
def get_html_color_for(cls, value):
r, g, b = cls.get_rgb_color_for(value)
return '#%02x%02x%02x' % (r, g, b)
@staticmethod
def get_not_mapped_yet_keyword():
return 'NOT MAPPED YET'
......
......@@ -597,18 +597,7 @@ def custom_css(request, slug):
slug = slug.replace(".", "-")
value = float(value)
lower = models.GlobalViralHostResponseValue.objects_mappable().filter(value__lte=value).order_by("-value").first()
upper = models.GlobalViralHostResponseValue.objects_mappable().filter(value__gte=value).order_by("value").first()
lower_hsv = lower.to_hsv()
upper_hsv = upper.to_hsv()
if lower.value == upper.value:
percentage = 0
else:
percentage = (value - lower.value) / (upper.value - lower.value)
hue = lower_hsv[0] * percentage + upper_hsv[0] * (1 - percentage)
sat = lower_hsv[1] * percentage + upper_hsv[1] * (1 - percentage)
lgt = lower_hsv[2] * percentage + upper_hsv[2] * (1 - percentage)
hue, sat, lgt = models.GlobalViralHostResponseValue.get_hsv_color_for(value)
# default color
s = 0.6
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment