forked from RaspberryPiFoundation/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_blockStyles.py
More file actions
182 lines (164 loc) · 5.41 KB
/
create_blockStyles.py
File metadata and controls
182 lines (164 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# !/usr/bin/env python
# Converts a single colour to primary, secondary and tertiary colours.
# Primary Colour - The colour given
# Secondary Colour - Lightens the primary colour by .8
# Tertiary Colour - Darkens the primary colour by .2
#
# Copyright 2012 Google Inc.
# https://developers.google.com/blockly/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Usage: create_blockStyles.py <fileName>
#
# fileName - Name of the file holding the map of style names to colours.
# Refer to blockStyles_example.json for an example input.
#
# Input: A json file with the keys being style names and the values
# being either a hue (number) or a hex value (string).
# Ex: {"styleName": 10, "styleName2": "#FFFFFF"}
#
# Output: A json file with the keys being style names and the values being an
# object holding primary, secondary, and tertiary colours in hex format.
# {"styleName":
# {"colourPrimary":"hexVal",
# "colourSecondary":"hexVal",
# "colourTertiary":"hexVal"
# }
# }
import math
import json
import sys
HSV_SATURATION = .45
BRIGHTNESS_VAL = .65 * 255
LIGHT_FACTOR = .8
DARK_FACTOR = .2
#Change HSV (Hue Saturation Value) to RGB
#This is adapted to python from the js version found here:
#https://blockly-demo.appspot.com/closure-library/closure/goog/colour/color.js
def hsvToRgb(h, s, brightness):
red = 0
green = 0
blue = 0
if (s == 0):
red = brightness
green = brightness
blue = brightness
else:
sextant = math.floor(h / 60)
remainder = (float(h) / float(60)) - sextant
val1 = brightness * (1 - s)
val2 = brightness * (1 - (s * remainder))
val3 = brightness * (1 - (s * (1 - remainder)))
if sextant == 1:
red = val2
green = brightness
blue = val1
elif sextant == 2:
red = val1
green = brightness
blue = val3
elif sextant == 3:
red = val1
green = val2
blue = brightness
elif sextant == 4:
red = val3
green = val1
blue = brightness
elif sextant == 5:
red = brightness
green = val1
blue = val2
elif sextant == 0:
red = brightness
green = val3
blue = val1
return [math.floor(red), math.floor(green), math.floor(blue)]
#Blend two rgb colours with the factor being the weight given to the first colour
#This is adapted to python from the js version found here:
#https://blockly-demo.appspot.com/closure-library/closure/goog/colour/color.js
def blend(rgb1, rgb2, factor):
factor = max(min(factor, 1), 0)
return [
round(rgb2[0] + factor * (rgb1[0] - rgb2[0])),
round(rgb2[1] + factor * (rgb1[1] - rgb2[1])),
round(rgb2[2] + factor * (rgb1[2] - rgb2[2]))
]
#Lightens a given rgb colour
#This is adapted to python from the js version found here:
#https://blockly-demo.appspot.com/closure-library/closure/goog/colour/color.js
def lighten(rgb, factor):
white = [255, 255, 255]
return blend(white, rgb, factor)
#Darkens a given rgb colour
#This is adapted to python from the js version found here:
#https://blockly-demo.appspot.com/closure-library/closure/goog/colour/color.js
def darken(rgb, factor):
black = [0, 0, 0];
return blend(black, rgb, factor)
#Converts rgb to hex
def rgbToHex(rgb):
#Add checks in here to make sure valid numbers
return '#%02x%02x%02x' % (rgb[0], rgb[1], rgb[2])
#Calculates the primary, secondary and tertiary colours for the block style
def findOtherColours(rgb):
colourPrimary = rgbToHex(rgb)
colourSecondary = rgbToHex(lighten(rgb, LIGHT_FACTOR))
colourTertiary = rgbToHex(darken(rgb, DARK_FACTOR))
return {
"colourPrimary": colourPrimary,
"colourSecondary": colourSecondary,
"colourTertiary": colourTertiary
}
# Converts a hex colour to rgb colour format
def hexToRgb(hexColour):
r = int(hexColour[1:3], 16)
g = int(hexColour[3:5], 16)
b = int(hexColour[5:7], 16)
return [r, g, b]
# Converts either a hue or hex colour to rgb colour format
def findRgbVal(colour):
try:
hue = int(colour)
rgb = hsvToRgb(hue, HSV_SATURATION, BRIGHTNESS_VAL)
except (TypeError, ValueError):
hexColour = colour
rgb = hexToRgb(hexColour)
return rgb
# Get info on the input file
def getFileInfo():
if (len(sys.argv) < 2):
print("Please provide a filename")
sys.exit()
fileName = sys.argv[1]
try:
jsonFile = open(fileName).read()
except IOError as err:
print('Could not find that file name')
sys.exit()
return (jsonFile, fileName)
# Creates a map with the keys being the style names and the values being an object
# holding colourPrimary, colourSecondary, colourTertiary
def createColourMap():
(jsonFile, fileName) = getFileInfo()
jsonData = json.loads(jsonFile)
colourObj = {}
for key in jsonData.keys():
rgbVal = findRgbVal(jsonData[key])
colourObj[key] = findOtherColours(rgbVal)
f= open("new_" + fileName,"w+")
f.write(json.dumps(colourObj, indent=2, sort_keys=True))
f.close()
createColourMap()