Rafał Pitoń 11 лет назад
Родитель
Сommit
28937c52d8
3 измененных файлов с 158 добавлено и 37 удалено
  1. 17 35
      misago/templatetags/utils.py
  2. 109 0
      misago/utils/colors.py
  3. 32 2
      templates/cranefly/threads/thread.html

+ 17 - 35
misago/templatetags/utils.py

@@ -1,5 +1,6 @@
 from django_jinja.library import Library
 from haystack.utils import Highlighter
+from misago.utils import colors
 from misago.utils.strings import short_string
 
 register = Library()
@@ -25,40 +26,21 @@ def highlight_result(text, query, length=500):
     return hl
 
 
-@register.global_function(name='color')
-def color_wheel(index):
-    while index > 15:
-        index -= 15
-    colors = (
-              (49, 130, 189),
-              (49, 163, 84),
-              (230, 85, 13),
-              (117, 107, 177),
-              (222, 45, 38),
-              (158, 202, 225),
-              (161, 217, 155),
-              (253, 174, 107),
-              (188, 189, 220),
-              (252, 146, 114),
-              (222, 235, 247),
-              (229, 245, 224),
-              (254, 230, 206),
-              (239, 237, 245),
-              (254, 224, 210),
-             )
-    return colors[index]
+@register.global_function(name='color_spin')
+def spin_color_filter(color, spin):
+    return colors.spin(color, spin)
 
 
-@register.global_function(name='colorhex')
-def color_hex(index):
-    color = color_wheel(index)
-    r = unicode(hex(color[0])[2:])
-    if len(r) == 0:
-        r = '0%s' % r
-    g = unicode(hex(color[1])[2:])
-    if len(g) == 0:
-        g = '0%s' % g
-    b = unicode(hex(color[2])[2:])
-    if len(b) == 0:
-        b = '0%s' % b
-    return r+g+b
+@register.global_function(name='color_desaturate')
+def desaturate_color_filter(color, steps, step, minimum=0.0):
+    return colors.desaturate(color, steps, step, minimum)
+
+
+@register.global_function(name='color_lighten')
+def lighten_color_filter(color, steps, step, maximum=100.0):
+    return colors.lighten(color, steps, step, maximum)
+
+
+@register.global_function(name='color_darken')
+def darken_color_filter(color, steps, step, minimum=0.0):
+    return colors.darken(color, steps, step, minimum)

+ 109 - 0
misago/utils/colors.py

@@ -0,0 +1,109 @@
+from colorsys import rgb_to_hsv, hsv_to_rgb
+
+def rgb_to_hex(r, g, b):
+    r = unicode(hex(int(r * 255))[2:]).zfill(2)
+    g = unicode(hex(int(g * 255))[2:]).zfill(2)
+    b = unicode(hex(int(b * 255))[2:]).zfill(2)
+    return r + g+ b
+
+
+def hex_to_rgb(color):
+    if len(color) == 6:
+        r, g, b = color[0:2], color[2:4], color[4:6]
+    elif len(color) == 3:
+        r, g, b = color[0], color[1], color[2]
+    else:
+        raise ValueError('"%s" is not correct HTML Hex Color.')
+
+    r, g, b = float(int(r, 16)), float(int(g, 16)), float(int(b, 16))
+
+    r /= 255.0
+    g /= 255.0
+    b /= 255.0
+    return r, g, b
+
+
+def spin(color, rad):
+    append_hex = False
+    if color[0] == '#':
+        append_hex = True
+        color = color[1:]
+
+    r, g, b = hex_to_rgb(color)
+    h, s, v = rgb_to_hsv(r, g, b)
+    if rad:
+        h += float(rad) / 360
+
+    r, g, b = hsv_to_rgb(h, s, v)
+
+    if append_hex:
+        return '#' + rgb_to_hex(r, g, b)
+    return rgb_to_hex(r, g, b)
+
+
+def desaturate(color, steps, step, minimum=0):
+    append_hex = False
+    if color[0] == '#':
+        append_hex = True
+        color = color[1:]
+
+    r, g, b = hex_to_rgb(color)
+    h, s, v = rgb_to_hsv(r, g, b)
+
+    minimum /= 100.0
+    scope = s - minimum
+    s = minimum + (scope / steps * (steps - step))
+
+    r, g, b = hsv_to_rgb(h, s, v)
+
+    if append_hex:
+        return '#' + rgb_to_hex(r, g, b)
+    return rgb_to_hex(r, g, b)
+
+
+def lighten(color, steps, step, maximum=100):
+    append_hex = False
+    if color[0] == '#':
+        append_hex = True
+        color = color[1:]
+
+    r, g, b = hex_to_rgb(color)
+
+    scope = maximum / 100.0 - min(r, g, b)
+    step = scope / steps * step
+
+    r += step
+    g += step
+    b += step
+
+    r = 1 if r > 1 else r
+    g = 1 if g > 1 else g
+    b = 1 if b > 1 else b
+
+    if append_hex:
+        return '#' + rgb_to_hex(r, g, b)
+    return rgb_to_hex(r, g, b)
+
+
+def darken(color, steps, step, minimum=0):
+    append_hex = False
+    if color[0] == '#':
+        append_hex = True
+        color = color[1:]
+
+    r, g, b = hex_to_rgb(color)
+
+    scope = minimum / 100.0 - max(r, g, b)
+    step = scope / steps * step
+
+    r += step
+    g += step
+    b += step
+
+    r = 0 if r < 0 else r
+    g = 0 if g < 0 else g
+    b = 0 if b < 0 else b
+
+    if append_hex:
+        return '#' + rgb_to_hex(r, g, b)
+    return rgb_to_hex(r, g, b)

+ 32 - 2
templates/cranefly/threads/thread.html

@@ -40,10 +40,40 @@
   {% endif %}
 
   {% if thread.has_poll %}
+  <div id="color_demo" style="background: #FFF; padding: 32px; overflow: auto;">
+    <h1>Random Colors</h1>
+    {% for i in range(12) %}
+    <div style="background-color: #{{ color_spin('049cdb', loop.index0 * 265) }}; margin: 4px; width:24px; height: 24px; border-radius: 3px; float: left;"></div>
+    {% endfor %}
+    <hr style="clear: both; border: none;">
+    {% for i in range(12) %}
+    <div style="background-color: #{{ color_lighten(color_spin('049cdb', loop.index0 * 265), 12, loop.index, 90) }}; margin: 4px; width:24px; height: 24px; border-radius: 3px; float: left;"></div>
+    {% endfor %}
+    <h1>Desaturate Colors</h1>
+    {% for i in range(32) %}
+    <div style="background-color: #{{ color_desaturate('049cdb', 32, loop.index) }}; margin: 4px; width:24px; height: 24px; border-radius: 3px; float: left;"></div>
+    {% endfor %}
+    <h1>Lighten Colors</h1>
+    {% for i in range(32) %}
+    <div style="background-color: #{{ color_lighten('049cdb', 32, loop.index) }}; margin: 4px; width:24px; height: 24px; border-radius: 3px; float: left;"></div>
+    {% endfor %}
+    <hr style="clear: both; border: none;">
+    {% for i in range(32) %}
+    <div style="background-color: #{{ color_desaturate(color_lighten('049cdb', 32, loop.index), 32, loop.index) }}; margin: 4px; width:24px; height: 24px; border-radius: 3px; float: left;"></div>
+    {% endfor %}
+    <h1>Darken Colors</h1>
+    {% for i in range(32) %}
+    <div style="background-color: #{{ color_darken('049cdb', 32, loop.index) }}; margin: 4px; width:24px; height: 24px; border-radius: 3px; float: left;"></div>
+    {% endfor %}
+    <hr style="clear: both; border: none;">
+    {% for i in range(32) %}
+    <div style="background-color: #{{ color_desaturate(color_darken('049cdb', 32, loop.index), 32, loop.index) }}; margin: 4px; width:24px; height: 24px; border-radius: 3px; float: left;"></div>
+    {% endfor %}
+  </div>
   <div class="thread-poll">
     <h2>{{ thread.poll.question }}</h2>
       {% for choice in thread.poll.option_set.all() %}
-      <div style="background: #{{ colorhex(loop.index0) }}">{{ choice.name }}</div>
+      <li><span style="background-color: #{{ color_desaturate(color_lighten('cf402e', loop.length, loop.index, 80), loop.length, loop.index, 20) }}; font-weight: bold; display: inline-block; width:18px; height: 18px; border-radius: 3px;"></span> {{ choice.name }}</li>
       {% endfor %}
     {% if poll_form %}
     <form action="" method="post">
@@ -52,7 +82,7 @@
     {% else %}
     <ul>
       {% for choice in thread.poll.option_set.all() %}
-      <li><div style="background: #{{ colorhex(loop.index0) }}">{{ choice.name }}</div></li>
+      <li><div style="background: #{{ spin_color('FF0000', 240 * loop.index0) }}">{{ choice.name }}</div></li>
       {% endfor %}
     </ul>
     {% endif %}