Skip to content

Commit 2953def

Browse files
committed
Add color to learning labels
1 parent ecfdde7 commit 2953def

6 files changed

Lines changed: 59 additions & 5 deletions

File tree

apps/learn/colors.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Tuple
2+
3+
BLACK = '000000'
4+
WHITE = 'FFFFFF'
5+
6+
7+
def get_rgb_from_hex(value: str) -> Tuple[int, int, int, int]:
8+
red = int(value[:2], base=16)
9+
green = int(value[2:4], base=16)
10+
blue = int(value[4:6], base=16)
11+
alpha = int(value[6:8], base=16) if len(value) > 6 else 255
12+
return red, green, blue, alpha
13+
14+
15+
def get_luminance(red, green, blue, alpha) -> int:
16+
luminance = 0.2126 * red + 0.7152 * green + 0.0722 * blue
17+
luminance *= alpha / 255
18+
return round(luminance)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.2.13 on 2022-06-22 18:39
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('learn', '0002_auto_20220602_1951'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='label',
15+
name='color',
16+
field=models.CharField(default='FF0000', max_length=8),
17+
),
18+
]

apps/learn/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
from django.db import models
22

3+
from . import colors
4+
35

46
class Label(models.Model):
57
class Meta:
68
ordering = ['name']
79

810
name = models.CharField(max_length=120)
911
slug = models.SlugField(max_length=120, unique=True)
12+
# Label color in hex mode
13+
color = models.CharField(max_length=8, default='FF0000')
1014

1115
def __str__(self):
1216
return self.name
1317

18+
@property
19+
def foreground_color(self):
20+
rgb_color = colors.get_rgb_from_hex(self.color)
21+
luminance = colors.get_luminance(*rgb_color)
22+
return colors.BLACK if luminance > 128 else colors.WHITE
23+
1424

1525
class Resource(models.Model):
1626
class Meta:

apps/learn/templates/learn/base.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,3 @@
55
{% block style %}
66
<link rel="stylesheet" href="{{ assets|get_asset_key:'learn/custom.min.css' }}">
77
{% endblock style %}
8-
9-
{% block js %}
10-
<script src="{{ assets|get_asset_key:'learn/custom.min.js' }}"></script>
11-
{% endblock js %}

apps/learn/templates/learn/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
<h1 class="title">Aprende Python</h1>
1212

13+
<p>En esta sección podrás encontrar recursos de aprendizaje Python.</p>
14+
1315
<ul>
1416
{% for label in labels %}
1517
<li><a href="{% url "learn:resources_by_label" label=label %}">{{ label }}</a></li>

apps/learn/templates/learn/resources_by_label.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@
88

99
{% block content %}
1010

11+
<nav class="breadcrumb" aria-label="breadcrumbs">
12+
<ul>
13+
<li><a href="{% url "homepage" %}"></a></li>
14+
<li><a href="{% url "learn:index" %}">Aprende Python</a></li>
15+
<li class="is-active"><a href="#" aria-current="page">{{ label }}</a></li>
16+
</ul>
17+
</nav>
18+
1119
<h1 class="title">{{ label }}</h1>
1220

1321
<ul>
1422
{% for resource in resources %}
1523
<li>
1624
<a href="{{ resource.url }}">{{ resource }}</a>
1725
{% for label in resource.labels.all %}
18-
<span class="tag is-primary"><a href="{% url "learn:resources_by_label" label=label %}">{{ label }}</a></span>
26+
<span class="tag" style="background-color: #{{ label.color }}">
27+
<a style="color: #{{ label.foreground_color }}" href="{% url "learn:resources_by_label" label=label %}">{{ label }}</a>
28+
</span>
1929
{% endfor %}
2030
</li>
2131
{% endfor %}

0 commit comments

Comments
 (0)