-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallicolor.ts
255 lines (236 loc) · 10.1 KB
/
callicolor.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
let Callistrip = neopixel.create(DigitalPin.P0, 12, NeoPixelMode.RGB)
const enum Richtung {
//% block="forward"
//% block.loc.de="Vorwärts"
forward = 1,
//% block="backward"
//% block.loc.de="Rückwärts"
backward = -1
}
const enum cbrightness {
//% block="100"
hp1 = 1,
//% block="80"
hp2 = 2,
//% block="60"
hp6 = 6,
//% block="40"
hp25 = 25,
//% block="20"
hp85 = 85
}
// gerade https://github.com/BrightWearables/pxt-microbit-brightboard
// gefunden. Sieht so ähnlich aus wie mein CalliColorboard.
// Von dort konnte ich den Code für den Colornumberpicker entleihen. THX!
//% color=#5882FA icon="\uf005" block="CalliColor"
namespace CalliColor {
let ccolors = [0xff0000, 0xFF7F00,0xFFFE00,0x7FFF00,0x00FF00,0x00FF7F,
0x00FFFE, 0x0040FF,0x0000FF,0x6000FF,0xFE00FF,0xFF0040]
//% block="show color %color at pixel %pixel || brightness %brightnes |\\%"
//% block.loc.de="zeige Farbe %color an Pixel %pixel || Helligkeit %brightnes |\\%"
//% color.shadow="CalliColorNumberPicker" color.defl=0xff0000
//% pixel.min=0 pixel.max=11 brightnes.defl=cbrightness.hp1
//% expandableArgumentMode="toggle"
export function ShowColorOnPixelbright(color: number, pixel:number, brightnes:cbrightness=cbrightness.hp1) {
let r,g,b:number
r = (color & 0xff0000) >> 16
g = (color & 0xff00) >> 8
b = (color & 0xff) // rgb Einzelfarbwerte extrahieren
r = Math.idiv(r, brightnes)
g = Math.idiv(g, brightnes)
b = Math.idiv(b, brightnes) // Helligkeit vermindern
color = (r << 16) + (g << 8) + b // Farbe zusammenbauen
control.waitMicros(150) //fix for ws2812E LEDs
Callistrip.setPixelColor(pixel, color)
Callistrip.show()
}
/**
* Sets a gradient between two colors
* @param startColor the start color
* @param endColor the end color
*/
//% blockId=lightsetgradient
//% block="show gradient from %startColor=CalliColorNumberPicker to %endColor=CalliColorNumberPicker ||startpixel %startp endpixel %endp"
//% block.loc.de="zeige Farbverlauf von %startColor=CalliColorNumberPicker nach %endColor=CalliColorNumberPicker ||von Pixel %startp nach Pixel %endp"
//% startColor.defl=0xff0000 endColor.defl=0x00ff00
//% startp.defl=0 endp.defl=11
//% expandableArgumentMode="toggle"
//% inlineInputMode=inline
export function setGradient(startColor: number, endColor: number, startp:number=0, endp:number=11) {
const sr = (startColor & 0xff0000) >> 16;
const sg = (startColor & 0xff00) >> 8;
const sb = (startColor & 0xff);
const er = (endColor & 0xff0000) >> 16;
const eg = (endColor & 0xff00) >> 8;
const eb = (endColor & 0xff);
if (Math.abs(endp - startp) < 3 ) {
startp=0;
endp=11;
}
const end = endp;
const start = startp
if (endp > startp) {
const n1 = end-start
for (let i = start; i <= end; ++i) {
let x = (i-start) / n1;
const ox = 1 - x;
const r = (sr * ox + er * x) | 0;
const g = (sg * ox + eg * x) | 0;
const b = (sb * ox + eb * x) | 0;
Callistrip.setPixelColor(i, ((r << 16) + (g << 8) + b))
}
} else {
const n1 = start-end
for (let i = start; i >= end; --i) {
let x = (i-end) / n1;
const ox = 1 - x;
const r = (er * ox + sr * x) | 0;
const g = (eg * ox + sg * x) | 0;
const b = (eb * ox + sb * x) | 0;
Callistrip.setPixelColor(i, ((r << 16) + (g << 8) + b))
}
}
Callistrip.show();
}
//% block="show rainbowcolor at pixel %pixel || brightness %brightnes |\\%"
//% block.loc.de="zeige Regenbogenfarbe an Pixel %pixel || Helligkeit %brightnes |\\%"
//% pixel.min=0 pixel.max=11 brightnes.defl=cbrightness.hp1
//% expandableArgumentMode="toggle"
export function ShowRainbowColorOnPixelbright(pixel:number, brightnes:cbrightness=cbrightness.hp1) {
let r,g,b,color:number
r = (ccolors[pixel] & 0xff0000) >> 16
g = (ccolors[pixel] & 0xff00) >> 8
b = (ccolors[pixel] & 0xff) // rgb Einzelfarbwerte extrahieren
r = Math.idiv(r, brightnes)
g = Math.idiv(g, brightnes)
b = Math.idiv(b, brightnes) // Helligkeit vermindern
color = (r << 16) + (g << 8) + b // Farbe zusammenbauen
control.waitMicros(150) //fix for ws2812E LEDs
Callistrip.setPixelColor(pixel, color)
Callistrip.show()
}
//% block="show colors on ring $color1 $color2 $color3 $color4 $color5 $color6 $color7 $color8 $color9 $color10 $color11 $color12"
//% block.loc.de="zeige Farben an Ring $color1 $color2 $color3 $color4 $color5 $color6 $color7 $color8 $color9 $color10 $color11 $color12"
//% color1.shadow="CalliColorNumberPicker" color1.defl=0xff0000
//% color2.shadow="CalliColorNumberPicker" color2.defl=0xFF7F00
//% color3.shadow="CalliColorNumberPicker" color3.defl=0xFFFE00
//% color4.shadow="CalliColorNumberPicker" color4.defl=0x7FFF00
//% color5.shadow="CalliColorNumberPicker" color5.defl=0x00FF00
//% color6.shadow="CalliColorNumberPicker" color6.defl=0x00FF7F
//% color7.shadow="CalliColorNumberPicker" color7.defl=0x00FFFE
//% color8.shadow="CalliColorNumberPicker" color8.defl=0x0040FF
//% color9.shadow="CalliColorNumberPicker" color9.defl=0x0000FF
//% color10.shadow="CalliColorNumberPicker" color10.defl=0x6000FF
//% color11.shadow="CalliColorNumberPicker" color11.defl=0xFE00FF
//% color12.shadow="CalliColorNumberPicker" color12.defl=0xFF0040
//% inlineInputMode=inline
export function ShowColorPixel(color1: number, color2: number, color3: number, color4: number, color5: number, color6: number, color7: number, color8: number, color9: number, color10: number, color11: number, color12: number) {
Callistrip.setPixelColor(0, color1)
Callistrip.setPixelColor(1, color2)
Callistrip.setPixelColor(2, color3)
Callistrip.setPixelColor(3, color4)
Callistrip.setPixelColor(4, color5)
Callistrip.setPixelColor(5, color6)
Callistrip.setPixelColor(6, color7)
Callistrip.setPixelColor(7, color8)
Callistrip.setPixelColor(8, color9)
Callistrip.setPixelColor(9, color10)
Callistrip.setPixelColor(10, color11)
Callistrip.setPixelColor(11, color12)
Callistrip.show()
}
/**
* Färbt alle LEDs in einer Farbe.
* Schwarz schaltet alle LEDs aus
*/
//% block="show ringcolor %color"
//% block.loc.de="zeige Ringfarbe %color"
//% color.shadow=CalliColorNumberPicker
//% color.defl='#4df243'
export function showCalliColor(color: number) {
Callistrip.showColor(color)
}
//% block="random color"
//% block.loc.de="Zufallsfarbe"
//% group=Farben
export function ShowRandomColor(): number {
return ccolors[randint(0, 11)]
}
/**
* Konvertiert Rot-, Grün- und Blauanteil in eine RGB Farbe
*/
//% blockId="Callineopixel_rgb" block="RGB: Rot %red|Grün %green|Blau %blue"
//% group=Farben
export function rgb(red: number, green: number, blue: number): number {
return ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
}
/**
* Converts a hue saturation luminosity value into a RGB color
* @param h Farbton from 0 to 360
* @param s Sättigung from 0 to 99
* @param l Helligkeit from 0 to 99
*/
//% blockId=calliHSL block="HSL: Hue %h|Saturation %s|brightness %l"
//% block.loc.de="HSL: Farbton %h|Sättigung %s|Helligkeit %l"
//% s.defl=99 l.defl=50
//% group=Farben
export function callihsl(h: number, s: number, l: number): number {
return neopixel.hsl(h,s,l);
}
/**
* Konvertiert den Farbnamen in eine Zahl
*/
//% blockId=CalliColor block="%c"
//% group=Farben
export function CalliColor(c: NeoPixelColors): number {
return c;
}
/**
* Legt die Helligkeit für den gesamten Neopixelring fest
*/
//% blockId=CalliBrightness block="set brightness to %c"
//% block.loc.de="setze Helligkeit auf %c"
//% c.defl=128
//% c.min=0 c.max=255
//% group="... mehr"
export function CalliBrightness(c: number){
Callistrip.setBrightness(c)
Callistrip.show()
}
/**
* Lässt die LEDs eine Stelle nach rechts oder links rotieren
*/
//% blockId=Callirotate
//% block="rotate pixel %r"
//% block.loc.de="Pixel rotieren %r"
export function Callirotate(r: Richtung) {
Callistrip.rotate(r)
Callistrip.show()
}
/**
* Zeigt ein Balkendiagramm basierend auf `wert` und `max`.
* Wenn `max` 0 ist, wird der Balken automatisch angepasst.
* @param wert aktueller zu zeichnender Wert
* @param max Maximalwert, z.B.: 255
*/
//% blockId=calli_show_bar_graph block="show bargraph from %wert|to %max"
//% block.loc.de="zeige Balkendiagramm von %wert|bis %max"
//% max.defl=1023
//% group="... mehr"
export function showCalliBarGraph(wert: number, max: number) {
Callistrip.showBarGraph(wert, max)
}
/**
* Custom color picker
*/
//% blockId=CalliColorNumberPicker block="%value"
//% blockHidden=true
//% shim=TD_ID
//% value.fieldEditor="colornumber" value.fieldOptions.decompileLiterals=true
//% weight=150
//% value.fieldOptions.colours='["#ffffff","#ff0000","#ffaa00","#ffdc00","#ffff00","#eaff00","#8eff00","#4df243","#42b87f","#00ffdc","#00dcff","#00a3ff","#0087ff","#acb3f3","#e0acfe","#a300ff","#ea00ff","#ff00e3","#fdd3f8","#f1d07e","#a8b5f5","#C3C6D8", "#f3f2da","#727474", "#000000"]'
//% value.fieldOptions.columns=5 value.fieldOptions.className='rgbColorPicker'
export function CalliColorNumberPicker(value: number) {
return value;
}
}