penRGB

Category:Turtle

penRGB(r,g,b,a)

Category: Turtle

Using RGBA values, sets the color of the pen used by the turtle for drawing lines and dots.

Explore the 256^3 possible colors you can choose from using the three required numeric parameters. The fourth optional parameter is the opacity of the color, a percentage between 0 and 1. The default pen color is black, penRGB(0,0,0), and the pen is by default in the down (drawing) position.

Examples


// Sets the color of the line the turtle draws behind it to orange.
penRGB(255,165,0);   
moveForward();

Example: Flag of France You can look up official RGB values for most country flags on Wikipedia.

// Flag of France - You can look up official RGB values for most country flags on Wikipedia.
penWidth(150);
penRGB(0,85,164);
penUp();
moveTo(0,75);
turnTo(90);
penDown();
moveForward(350);

penRGB(255,255,255);
penUp();
moveTo(0,225);
turnTo(90);
penDown();
moveForward(350);

penRGB(239,65,53);
penUp();
moveTo(0,375);
turnTo(90);
penDown();
moveForward(350);

Example: Random Art Draw random lines in random colors.

// Draw random lines in random colors.
button("ChangeIt", "Click Me");
onEvent("ChangeIt", "click", function() {
  penRGB(randomNumber(0,255),randomNumber(0,255),randomNumber(0,255), randomNumber(0,100)/100);
  penWidth(randomNumber(10,50));
  moveTo(randomNumber(0,320),randomNumber(0,450));
});

Example: User Color Choice Dots Prompts the user for r, g, b values and draws a random size dot at a random location. A console warning is generated if the user enters an invalid r or g or b value. Negative values are set to 0 and values greater than 255 are set to 255.

// Prompts the user for r, g, b values and draws a random size dot at a random location. 
// A console warning is generated if the user enters an invalid r or g or b value. 
// Negative values are set to 0 and values greater than 255 are set to 255.
textLabel("RedLabel", "Red (0-255)");
textInput("RedID", "0");
textLabel("GreenLabel", "Green (0-255)");
textInput("GreenID", "0");
textLabel("BlueLabel", "Blue (0-255)");
textInput("BlueID", "0");
onEvent("RedID", "change", function() {
  penRGB(getText("RedID"), getText("BlueID"), getText("GreenID"));
  penUp();
  moveTo(randomNumber(0,320),randomNumber(0,450));
  dot(randomNumber(10,50));
});
onEvent("GreenID", "change", function() {
  penRGB(getText("RedID"), getText("BlueID"), getText("GreenID"));
  penUp();
  moveTo(randomNumber(0,320),randomNumber(0,450));
  dot(randomNumber(10,50));
});
onEvent("BlueID", "change", function() {
  penRGB(getText("RedID"), getText("BlueID"), getText("GreenID"));
  penUp();
  moveTo(randomNumber(0,320),randomNumber(0,450));
  dot(randomNumber(10,50));
});

Syntax

penRGB(r, g, b, a);

Parameters

Name Type Required? Description
r Number Yes The amount of red (0-255) in the pen used by the turtle for drawing lines and dots.
g Number Yes The amount of green (0-255) in the pen used by the turtle for drawing lines and dots.
b Number Yes The amount of blue (0-255) in the pen used by the turtle for drawing lines and dots.
a Number No The opacity, a number between 0.0 (fully transparent) and 1.0 (fully opaque), of the color used by the turtle for drawing lines and dots. Default is 1.0.

Returns

No return value. Modifies turtle drawing only.

Tips

  • penUp() causes no line to be drawn.
  • Turtle drawing commands are not effected by the show() and hide() commands, which control if the turtle icon is displayed or not.
  • Recall Unit 1 lessons about rgb color values.
  • To choose a color by name or hex value use [penColor(color)] (/applab/docs/penColor) which takes one string parameter instead of numeric parameters.

Found a bug in the documentation? Let us know at documentation@code.org

Found a bug in the documentation? Let us know at documentation@code.org