Skip to content

Commit

Permalink
Refact road colour generation script
Browse files Browse the repository at this point in the history
  • Loading branch information
pnorman committed Sep 11, 2015
1 parent c6269ed commit 57f5037
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 230 deletions.
31 changes: 16 additions & 15 deletions roads.mss
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* For the main linear features, such as roads and railways. */

//road colors for major roads were generated with scripts/generate_road_colors.py
@motorway-fill: #e892a2; // lch(70, 35, 10) Conversion error on moving from lch to rgb: 0.5 lch(-0.3, -0.5, -0.9)
@trunk-fill: #f9b29c; // lch(79, 33, 42) Conversion error on moving from lch to rgb: 0.8 lch(-0.4, -1.5, 1.1)
@primary-fill: #fcd6a4; // lch(88, 31, 74) Conversion error on moving from lch to rgb: 1.8 lch(-0.3, -0.8, 4.0)
@secondary-fill: #f7fabf; // lch(97, 29, 106) Conversion error on moving from lch to rgb: 1.8 lch(-0.2, 0.7, 4.4)
@motorway-fill: #e892a2; // Lch(70,35,10), error 0.5
@trunk-fill: #f9b29c; // Lch(79,33,42), error 0.7
@primary-fill: #fcd6a4; // Lch(88,31,74), error 1.7
@secondary-fill: #f7fabf; // Lch(97,29,106), error 1.7
@tertiary-fill: #ffffff;
@residential-fill: #ffffff;
@service-fill: @residential-fill;
Expand All @@ -23,15 +23,16 @@
@taxiway-fill: @aeroway-fill;
@helipad-fill: @aeroway-fill;

@motorway-low-zoom: #e66e89; // lch(62, 50, 10) Conversion error on moving from lch to rgb: 0.6 lch(-0.4, -0.4, -1.1)
@trunk-low-zoom: #fa9476; // lch(72, 50, 42) Conversion error on moving from lch to rgb: 0.8 lch(-0.5, -1.9, 0.7)
@primary-low-zoom: #f8c171; // lch(82, 50, 74) Conversion error on moving from lch to rgb: 2.1 lch(-0.5, -1.1, 3.4)
@motorway-low-zoom: #e66e89; // Lch(62,50,10), error 0.7
@trunk-low-zoom: #fa9476; // Lch(72,50,42), error 0.8
@primary-low-zoom: #f8c171; // Lch(82,50,74), error 2.1
@secondary-low-zoom: #e6ef89; // Lch(92,50,106), error 2.2

@default-casing: white;
@motorway-casing: #dc2a67; // lch(50, 70, 10) Conversion error on moving from lch to rgb: 1.0 lch(-0.7, -0.1, -1.2)
@trunk-casing: #c84e2f; // lch(50, 65, 42) Conversion error on moving from lch to rgb: 0.8 lch(-0.7, -1.8, 0.0)
@primary-casing: #a06b00; // lch(50, 60, 74) Conversion error on moving from lch to rgb: 1.6 lch(-0.4, -1.7, 2.2)
@secondary-casing: #707d05; // lch(50, 55, 106) Conversion error on moving from lch to rgb: 1.9 lch(-0.2, 1.7, 3.2)
@motorway-casing: #dc2a67; // Lch(50,70,10), error 1.1
@trunk-casing: #c84e2f; // Lch(50,65,42), error 0.7
@primary-casing: #a06b00; // Lch(50,60,74), error 1.6
@secondary-casing: #707d05; // Lch(50,55,106), error 1.9
@tertiary-casing: #8f8f8f;
@residential-casing: #bbb;
@road-casing: @residential-casing;
Expand All @@ -45,10 +46,10 @@
@bridleway-casing: @default-casing;
@track-casing: @default-casing;

@motorway-low-zoom-casing: #d63668; // lch(50, 65, 10) Conversion error on moving from lch to rgb: 0.9 lch(-0.6, -0.2, -1.2)
@trunk-low-zoom-casing: #d76444; // lch(56.7, 60, 42) Conversion error on moving from lch to rgb: 0.8 lch(-0.6, -1.9, 0.3)
@primary-low-zoom-casing: #c48e37; // lch(63.3, 55, 74) Conversion error on moving from lch to rgb: 2.0 lch(-0.5, -1.0, 2.9)
@secondary-low-zoom-casing: #a8b14f; // lch(70, 50, 106) Conversion error on moving from lch to rgb: 2.1 lch(-0.3, 1.4, 3.7)
@motorway-low-zoom-casing: #c24e6b; // Lch(50,50,10), error 0.8
@trunk-low-zoom-casing: #cf6649; // Lch(56,55,42), error 0.9
@primary-low-zoom-casing: #c38a27; // Lch(62,60,74), error 2.1
@secondary-low-zoom-casing: #9eae23; // Lch(68,65,106), error 2.3

@unimportant-road: @residential-casing;

Expand Down
11 changes: 6 additions & 5 deletions scripts/create_standard_shields
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ primary:#d5b284
secondary:#c6ca90
tertiary:#c6c6c6'

#motorway: lch(65, 30, 10) Conversion error on moving from lch to rgb: 0.4 lch(-0.3, -0.4, -0.9)
#trunk: lch(70, 30, 42) Conversion error on moving from lch to rgb: 0.8 lch(-0.3, -1.4, 1.1)
#primary: lch(75, 30, 74) Conversion error on moving from lch to rgb: 1.7 lch(-0.3, -0.8, 3.9)
#secondary: lch(80, 30, 106) Conversion error on moving from lch to rgb: 1.7 lch(-0.2, 0.7, 4.3)
#tertiary: lch(80, 0, 0) Conversion error on moving from lch to rgb: 0.0 lch(-0.0, 0.0, 322.5)
# motorway: Lch(65,30,10), error 0.5
# trunk: Lch(70,30,42), error 0.7
# primary: Lch(75,30,74), error 1.6
# secondary: Lch(80,30,106), error 1.8
# tertiary: Lch(80,0,0), error 0.1




Expand Down
210 changes: 0 additions & 210 deletions scripts/generate_road_colors.py

This file was deleted.

94 changes: 94 additions & 0 deletions scripts/generate_road_colours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from colormath.color_conversions import convert_color
from colormath.color_objects import LabColor, LCHabColor, sRGBColor
from colormath.color_diff import delta_e_cie2000
import numpy

from collections import OrderedDict, namedtuple

class Color:
def __init__(self, lch_tuple):
self.m_lch = LCHabColor(*lch_tuple)

def lch(self):
return "Lch({:.0f},{:.0f},{:.0f})".format(*(self.m_lch.get_value_tuple()))

def rgb(self):
rgb = convert_color(self.m_lch, sRGBColor)
if (rgb.rgb_r != rgb.clamped_rgb_r or rgb.rgb_g != rgb.clamped_rgb_g or rgb.rgb_b != rgb.clamped_rgb_b):
raise Exception("Colour {} is outside sRGB".format(self.lch()))
return rgb.get_rgb_hex()

def rgb_error(self):
return delta_e_cie2000(convert_color(self.m_lch, LabColor),
convert_color(sRGBColor.new_from_rgb_hex(self.rgb()), LabColor))

road_classes = ["motorway", "trunk", "primary", "secondary"]
colour_divisions = len(road_classes) - 1
hues = OrderedDict()

# The minimum and maximum hue for the road colours
# Because hue is a circle, it may be needed to add/subtract 360 to the min or
# max when changing them

min_h = 10
max_h = 106
delta_h = (max_h - min_h) / colour_divisions

h = min_h
for name in road_classes:
hues[name] = h
h = (h + delta_h) % 360

print hues
# A class to hold information for each line
ColourInfo = namedtuple("ColourInfo", ["start_l", "end_l", "start_c", "end_c"])

line_colour_infos = OrderedDict()

# The saturation and lightness for each type of line
line_colour_infos["fill"] = ColourInfo(start_l = 70, end_l = 97, start_c = 35, end_c = 29)
line_colour_infos["low-zoom"] = ColourInfo(start_l = 62, end_l = 92, start_c = 50, end_c = 50)

line_colour_infos["casing"] = ColourInfo(start_l = 50, end_l = 50, start_c = 70, end_c = 55)
line_colour_infos["low-zoom-casing"] = ColourInfo(start_l = 50, end_l = 70, start_c = 50, end_c = 65)

# Colours for the MSS
colours = OrderedDict()

for line_name, line_colour_info in line_colour_infos.iteritems():
c = line_colour_info.start_c
delta_c = (line_colour_info.end_c - line_colour_info.start_c) / colour_divisions
l = line_colour_info.start_l
delta_l = (line_colour_info.end_l - line_colour_info.start_l) / colour_divisions

for name in road_classes:
colours[name + "-" + line_name] = Color((l, c, hues[name]))
c += delta_c
l += delta_l

for name, colour in colours.iteritems():
print "@{name}: {rgb}; // {lch}, error {delta:.1f}".format(name = name, rgb = colour.rgb(), lch = colour.lch(), delta = colour.rgb_error())

# Generate colours for the shields
shield_colour_info = ColourInfo(start_l = 65, end_l = 80, start_c = 30, end_c = 30)
shield_colours = OrderedDict()


c = shield_colour_info.start_c
delta_c = (shield_colour_info.end_c - shield_colour_info.start_c) / colour_divisions
l = shield_colour_info.start_l
delta_l = (shield_colour_info.end_l - shield_colour_info.start_l) / colour_divisions

for name in road_classes:
shield_colours[name] = Color((l, c, hues[name]))
c += delta_c
l += delta_l

shield_colours["tertiary"] = Color((shield_colour_info.end_l, 0, 0))

print "\n\nRoad shield information\n\n"
for name, colour in shield_colours.iteritems():
print "{name}:{rgb}".format(name = name, rgb = colour.rgb())

for name, colour in shield_colours.iteritems():
print "# {name}: {lch}, error {delta:.1f}".format(name = name, lch = colour.lch(), delta = colour.rgb_error())

0 comments on commit 57f5037

Please sign in to comment.