Skip to content

Commit 38a9dcf

Browse files
committed
Fix issue #4793 Replaced Duplicate classes
1 parent 74b88f5 commit 38a9dcf

File tree

1 file changed

+32
-139
lines changed

1 file changed

+32
-139
lines changed

js/blocks/SensorsBlocks.js

Lines changed: 32 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -494,158 +494,47 @@ function setupSensorsBlocks(activity) {
494494
}
495495

496496
/**
497-
* Represents a block that returns the blue component of the pixel under the mouse or turtle.
498-
* @extends {ValueBlock}
499-
*/
500-
class GetBlueBlock extends ValueBlock {
501-
/**
502-
* Constructs a new GetBlueBlock instance.
503-
*/
504-
constructor() {
505-
super("getblue", _("blue"));
497+
* Base class for color component blocks (red, green, blue).
498+
* Represents a block that returns the red, green or blue component of the pixel under the mouse or turtle.
499+
* @extends {ValueBlock}
500+
*/
501+
class GetColorComponentBlock extends ValueBlock {
502+
/**
503+
* Constructs a new color component block instance.
504+
* @param {string} name - The internal block name (e.g., "getred").
505+
* @param {string} label - The display label (e.g., _("red")).
506+
* @param {number} index - The RGB component index (0=red, 1=green, 2=blue).
507+
* @param {string} helpMouse - Help text for mouse context.
508+
* @param {string} helpTurtle - Help text for turtle context.
509+
*/
510+
constructor(name, label, index, helpMouse, helpTurtle) {
511+
super(name, label);
512+
this.colorIndex = index;
506513
this.setPalette("sensors", activity);
507514
this.parameter = true;
508-
if (_THIS_IS_MUSIC_BLOCKS_) {
509-
this.setHelpString([
510-
_("The Get blue block returns the blue component of the pixel under the mouse."),
511-
"documentation",
512-
""
513-
]);
514-
} else {
515-
this.setHelpString([
516-
_("The Get blue block returns the blue component of the pixel under the turtle."),
517-
"documentation",
518-
""
519-
]);
520-
}
521-
}
522-
523-
/**
524-
* Updates the parameter value of the block.
525-
* @param {Object} logo - The logo object.
526-
* @param {number} turtle - The identifier of the turtle.
527-
* @param {number} blk - The identifier of the block.
528-
* @returns {number} - The updated parameter value representing the blue component.
529-
*/
530-
updateParameter(logo, turtle, blk) {
531-
return toFixed2(activity.blocks.blockList[blk].value);
532-
}
533515

534-
/**
535-
* Retrieves the argument value of the block.
536-
* @param {Object} logo - The logo object.
537-
* @param {number} turtle - The identifier of the turtle.
538-
* @returns {number} - The argument value representing the blue component.
539-
*/
540-
arg(logo, turtle) {
541-
let colorString = activity.turtles.getTurtle(turtle).painter.canvasColor;
542-
if (colorString[2] === "#") colorString = hex2rgb(colorString.split("#")[1]);
543-
const obj = colorString.split("(")[1].split(",");
544-
return parseInt(Number(obj[0]) / 2.55);
545-
}
546-
}
547-
548-
/**
549-
* Represents a block that returns the green component of the pixel under the mouse or turtle.
550-
* @extends {ValueBlock}
551-
*/
552-
class GetGreenBlock extends ValueBlock {
553-
/**
554-
* Constructs a new GetGreenBlock instance.
555-
*/
556-
constructor() {
557-
super("getgreen", _("green"));
558-
this.setPalette("sensors", activity);
559-
this.parameter = true;
560516
if (_THIS_IS_MUSIC_BLOCKS_) {
561-
this.setHelpString([
562-
_("The Get green block returns the green component of the pixel under the mouse."),
563-
"documentation",
564-
""
565-
]);
517+
this.setHelpString([helpMouse, "documentation", ""]);
566518
} else {
567-
this.setHelpString([
568-
_("The Get green block returns the green component of the pixel under the turtle."),
569-
"documentation",
570-
""
571-
]);
519+
this.setHelpString([helpTurtle, "documentation", ""]);
572520
}
573521
}
574522

575-
/**
576-
* Updates the parameter value of the block.
577-
* @param {Object} logo - The logo object.
578-
* @param {number} turtle - The identifier of the turtle.
579-
* @param {number} blk - The identifier of the block.
580-
* @returns {number} - The updated parameter value representing the green component.
581-
*/
582523
updateParameter(logo, turtle, blk) {
583524
return toFixed2(activity.blocks.blockList[blk].value);
584525
}
585526

586-
/**
587-
* Retrieves the argument value of the block.
588-
* @param {Object} logo - The logo object.
589-
* @param {number} turtle - The identifier of the turtle.
590-
* @returns {number} - The argument value representing the green component.
591-
*/
592527
arg(logo, turtle) {
593528
let colorString = activity.turtles.getTurtle(turtle).painter.canvasColor;
594-
if (colorString[1] === "#") colorString = hex2rgb(colorString.split("#")[1]);
595-
const obj = colorString.split("(")[1].split(",");
596-
return parseInt(Number(obj[0]) / 2.55);
597-
}
598-
}
599529

600-
/**
601-
* Represents a block that returns the red component of the pixel under the mouse or turtle.
602-
* @extends {ValueBlock}
603-
*/
604-
class GetRedBlock extends ValueBlock {
605-
/**
606-
* Constructs a new GetRedBlock instance.
607-
*/
608-
constructor() {
609-
super("getred", _("red"));
610-
this.setPalette("sensors", activity);
611-
this.parameter = true;
612-
if (_THIS_IS_MUSIC_BLOCKS_) {
613-
this.setHelpString([
614-
_("The Get red block returns the red component of the pixel under the mouse."),
615-
"documentation",
616-
""
617-
]);
618-
} else {
619-
this.setHelpString([
620-
_("The Get red block returns the red component of the pixel under the turtle."),
621-
"documentation",
622-
""
623-
]);
530+
// Handle hex and rgb color formats
531+
if (colorString.includes("#")) {
532+
colorString = hex2rgb(colorString.split("#")[1]);
624533
}
625-
}
626-
627-
/**
628-
* Updates the parameter value of the block.
629-
* @param {Object} logo - The logo object.
630-
* @param {number} turtle - The identifier of the turtle.
631-
* @param {number} blk - The identifier of the block.
632-
* @returns {number} - The updated parameter value representing the red component.
633-
*/
634-
updateParameter(logo, turtle, blk) {
635-
return toFixed2(activity.blocks.blockList[blk].value);
636-
}
637534

638-
/**
639-
* Retrieves the argument value of the block.
640-
* @param {Object} logo - The logo object.
641-
* @param {number} turtle - The identifier of the turtle.
642-
* @returns {number} - The argument value representing the red component.
643-
*/
644-
arg(logo, turtle) {
645-
let colorString = activity.turtles.getTurtle(turtle).painter.canvasColor;
646-
if (colorString[0] === "#") colorString = hex2rgb(colorString.split("#")[1]);
647535
const obj = colorString.split("(")[1].split(",");
648-
return parseInt(Number(obj[0]) / 2.55);
536+
const component = Number(obj[this.colorIndex]);
537+
return parseInt(component / 2.55);
649538
}
650539
}
651540

@@ -995,7 +884,7 @@ function setupSensorsBlocks(activity) {
995884
if (
996885
logo.inStatusMatrix &&
997886
activity.blocks.blockList[activity.blocks.blockList[blk].connections[0]].name ===
998-
"print"
887+
"print"
999888
) {
1000889
logo.statusFields.push([blk, "toascii"]);
1001890
} else {
@@ -1063,9 +952,13 @@ function setupSensorsBlocks(activity) {
1063952
}
1064953
}
1065954

1066-
new GetBlueBlock().setup(activity);
1067-
new GetGreenBlock().setup(activity);
1068-
new GetRedBlock().setup(activity);
955+
[
956+
{ name: "getred", label: _("red"), index: 0, helpMouse: _("The Get red block returns the red component of the pixel under the mouse."), helpTurtle: _("The Get red block returns the red component of the pixel under the turtle.") },
957+
{ name: "getgreen", label: _("green"), index: 1, helpMouse: _("The Get green block returns the green component of the pixel under the mouse."), helpTurtle: _("The Get green block returns the green component of the pixel under the turtle.") },
958+
{ name: "getblue", label: _("blue"), index: 2, helpMouse: _("The Get blue block returns the blue component of the pixel under the mouse."), helpTurtle: _("The Get blue block returns the blue component of the pixel under the turtle.") }
959+
].forEach(cfg => {
960+
new GetColorComponentBlock(cfg.name, cfg.label, cfg.index, cfg.helpMouse, cfg.helpTurtle).setup(activity);
961+
});
1069962
new GetColorPixelBlock().setup(activity);
1070963
new ToASCIIBlock().setup(activity);
1071964
new KeyboardBlock().setup(activity);
@@ -1087,4 +980,4 @@ function setupSensorsBlocks(activity) {
1087980

1088981
if (typeof module !== "undefined" && module.exports) {
1089982
module.exports = { setupSensorsBlocks };
1090-
}
983+
}

0 commit comments

Comments
 (0)