Add event hooks to loading animations and controllers

Optimize limit to palette function
This commit is contained in:
JannisX11 2023-01-24 18:50:38 +01:00
parent f61c849f43
commit 0dc390b880
2 changed files with 13 additions and 11 deletions

View File

@ -1453,6 +1453,7 @@ const Animator = {
animation.select()
}
new_animations.push(animation)
Blockbench.dispatchEvent('load_animation', {animation, json});
}
} else if (typeof json.animation_controllers === 'object') {
for (let ani_name in json.animation_controllers) {
@ -1470,6 +1471,7 @@ const Animator = {
controller.select();
}
new_animations.push(controller)
Blockbench.dispatchEvent('load_animation_controller', {animation_controller: controller, json});
}
}
return new_animations

View File

@ -533,21 +533,21 @@ BARS.defineActions(function() {
Painter.scanCanvas(ctx, 0, 0, canvas.width, canvas.height, (x, y, pixel) => {
if (pixel[3] < 4) return;
let nearest = [];
let smallest_distance = Infinity;
let nearest_color = null;
let pixel_color = {_r: pixel[0], _g: pixel[1], _b: pixel[2]};
for (let key in palette) {
let color = palette[key];
let distance = colorDistance(color, {_r: pixel[0], _g: pixel[1], _b: pixel[2]});
if (distance < 100) {
nearest.push({distance, color})
let distance = colorDistance(color, pixel_color);
if (distance < smallest_distance) {
smallest_distance = distance;
nearest_color = color;
}
}
if (!nearest.length) return;
nearest.sort((a, b) => {
return a.distance - b.distance;
})
pixel[0] = nearest[0].color._r;
pixel[1] = nearest[0].color._g;
pixel[2] = nearest[0].color._b;
if (!nearest_color) return;
pixel[0] = nearest_color._r;
pixel[1] = nearest_color._g;
pixel[2] = nearest_color._b;
})
}, {no_undo: true});