Skip to content

Commit 2a0fac6

Browse files
authored
[Color conversion] Add expressions for calculating luminance. (#700)
Added expressions for calculating the luminance of colors according to WCAG 2.0 standards. (Thanks @Ahnaf30e!)
1 parent 42ff24c commit 2a0fac6

File tree

1 file changed

+86
-8
lines changed

1 file changed

+86
-8
lines changed

extensions/reviewed/ColorConversion.json

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"iconUrl": "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiBpZD0ibWRpLWludmVydC1jb2xvcnMiIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0Ij48cGF0aCBkPSJNMTIsMTkuNThWMTkuNThDMTAuNCwxOS41OCA4Ljg5LDE4Ljk2IDcuNzYsMTcuODNDNi42MiwxNi42OSA2LDE1LjE5IDYsMTMuNThDNiwxMiA2LjYyLDEwLjQ3IDcuNzYsOS4zNEwxMiw1LjFNMTcuNjYsNy45M0wxMiwyLjI3VjIuMjdMNi4zNCw3LjkzQzMuMjIsMTEuMDUgMy4yMiwxNi4xMiA2LjM0LDE5LjI0QzcuOSwyMC44IDkuOTUsMjEuNTggMTIsMjEuNThDMTQuMDUsMjEuNTggMTYuMSwyMC44IDE3LjY2LDE5LjI0QzIwLjc4LDE2LjEyIDIwLjc4LDExLjA1IDE3LjY2LDcuOTNaIiAvPjwvc3ZnPg==",
88
"name": "ColorConversion",
99
"previewIconUrl": "https://resources.gdevelop-app.com/assets/Icons/invert-colors.svg",
10-
"shortDescription": "Expressions to convert color values between various formats (RGB, HSV, HSL, named colors) and get colors between 2 others.",
11-
"version": "1.2.1",
12-
"description": "Expressions to convert color values between various formats (RGB, HSV, HSL, named colors) and get colors between 2 others.",
10+
"shortDescription": "Expressions to convert color values between various formats (RGB, HSV, HSL, named colors), calculate luminance according to WCAG 2.0 standards, and to blend two colors.",
11+
"version": "1.3.1",
12+
"description": "Expressions to convert color values between various formats (RGB, HSV, HSL, named colors), calculate luminance according to WCAG 2.0 standards, and to blend two colors.",
1313
"origin": {
1414
"identifier": "ColorConversion",
1515
"name": "gdevelop-extension-store"
@@ -21,12 +21,16 @@
2121
"rgb",
2222
"hsl",
2323
"hsv",
24-
"hsb"
24+
"hsb",
25+
"luminance",
26+
"wcag",
27+
"blend"
2528
],
2629
"authorIds": [
2730
"gqDaZjCfevOOxBYkK6zlhtZnXCg1",
2831
"AlZ3D1xkH0QDao7T37VZZUeYNpn1",
29-
"IWykYNRvhCZBN3vEgKEbBPOR3Oc2"
32+
"IWykYNRvhCZBN3vEgKEbBPOR3Oc2",
33+
"onPsboRtDkUHNOsx7OPr8R8G1oj2"
3034
],
3135
"dependencies": [],
3236
"eventsFunctions": [
@@ -81,8 +85,82 @@
8185
"objectGroups": []
8286
},
8387
{
84-
"description": "Apply a weighted mean between 2 RGB colors.",
85-
"fullName": "RGB mean",
88+
"description": "Calculate luminance of a RGB color. Example input: \"0;128;255\".",
89+
"fullName": "Luminance from RGB",
90+
"functionType": "Expression",
91+
"name": "RgbToLuminance",
92+
"sentence": "",
93+
"events": [
94+
{
95+
"type": "BuiltinCommonInstructions::JsCode",
96+
"inlineCode": [
97+
"function toLuminance(color) {",
98+
" // Convert the color string to an array of RGB values",
99+
" let [r, g, b] = color.split(\";\");",
100+
" let luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;",
101+
" return luminance;",
102+
"}",
103+
"",
104+
"const color = eventsFunctionContext.getArgument(\"color\");",
105+
"eventsFunctionContext.returnValue = toLuminance(color);"
106+
],
107+
"parameterObjects": "",
108+
"useStrict": true,
109+
"eventsSheetExpanded": false
110+
}
111+
],
112+
"expressionType": {
113+
"type": "expression"
114+
},
115+
"parameters": [
116+
{
117+
"description": "RGB color",
118+
"name": "color",
119+
"type": "color"
120+
}
121+
],
122+
"objectGroups": []
123+
},
124+
{
125+
"description": "Calculate luminance of a hexadecimal color. Example input: \"0459AF\".",
126+
"fullName": "Luminance from hexadecimal",
127+
"functionType": "Expression",
128+
"name": "HexToLuminance",
129+
"sentence": "",
130+
"events": [
131+
{
132+
"type": "BuiltinCommonInstructions::JsCode",
133+
"inlineCode": [
134+
"function hexToLuminance(color) {",
135+
" // Convert the color string to an array of RGB values",
136+
" let [r, g, b] = color.slice(1).match(/.{2}/g).map(x => parseInt(x, 16));",
137+
" let luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;",
138+
" return luminance;",
139+
"}",
140+
"",
141+
"const color = eventsFunctionContext.getArgument(\"color\");",
142+
"eventsFunctionContext.returnValue = hexToLuminance(color);"
143+
],
144+
"parameterObjects": "",
145+
"useStrict": true,
146+
"eventsSheetExpanded": true
147+
}
148+
],
149+
"expressionType": {
150+
"type": "expression"
151+
},
152+
"parameters": [
153+
{
154+
"description": "Hex value",
155+
"name": "color",
156+
"type": "string"
157+
}
158+
],
159+
"objectGroups": []
160+
},
161+
{
162+
"description": "Blend two RGB colors by applying a weighted mean.",
163+
"fullName": "Blend RGB colors",
86164
"functionType": "StringExpression",
87165
"name": "RgbMean",
88166
"sentence": "",
@@ -134,7 +212,7 @@
134212
},
135213
{
136214
"description": "Ratio",
137-
"longDescription": "0 gives the first color, 1 gives the second color",
215+
"longDescription": "Range: 0 to 1, where 0 gives the first color and 1 gives the second color",
138216
"name": "Ratio",
139217
"type": "expression"
140218
}

0 commit comments

Comments
 (0)