Vous êtes sur la page 1sur 5

Lesson5:Colors

Thefirstfewlessonstaughtushowtowriteprogramsthatdrawpictures.Sofar,allofourprogramsdrawthe
blackandwhitepictures.Inthislesson,wearegoingtoaddcolorstoourpictures.
Logogivesyoutwowaystopickacolor.Onewayistopickacolorfromasetof16commonlyusedcolors.
Anotherwayistocreateyourowncolorbymixingdifferentamountsofred,green,andblue.Thisgivesyoua
choiceofover16milliondifferentcolors.

PenColorandScreenColor
YoucanchangethecolorofthelinesthattheturtledrawswiththeSETPENCOLORcommand.Youcan
changethecolorofthescreen(orbackground)withtheSETSCREENCOLORcommand.
Command
SETPENCOLOR
color

Example
SETPENCOLOR2

WhatHappens
Setsthecolorthattheturtledrawstogreen.

SETSCREENCOLOR SETSCREENCOLOR Fillstheentirescreenwithgreen.Anythingonthescreenwill


color
2
beerased.Theturtleisnotmoved.

ColorIndex
Theeasywaytochangethecoloroftheturtle'spenistogiveSETPENCOLORanumberfrom015.Each
numberwillsettheturtle'spentoadifferentcolor,giveninthetablebelow.Thenumberiscalledthe"color
index".
ColorIndex
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

ColorName

[RGB]
black
[000]
blue
[00255]
green
[02550]
cyan(lightblue)
[0255255]
red
[25500]
magenta(reddishpurple) [2550255]
yellow
[2552550]
white
[255255255]
brown
[1559659]
lightbrown
[19713618]
darkgreen
[10016264]
darkishblue
[120187187]
tan
[255149119]
plum(purplish)
[144113208]
orange
[2551630]
gray
[183183183]

Color

Activity:Drawabluesquareonayellowbackground.

SETSCREENCOLOR6
SETPENCOLOR1
REPEAT4[FORWARD100RIGHT90]

WhatwouldhappenifyouswitchedthenumberthatyougavetoSETSCREENCOLORand
SETPENCOLOR?Tryitandsee.
WhatwouldhappenifyougaveSETSCREENCOLORandSETPENCOLORdifferentnumbersall
together?Tryitandsee.
Whathappensifyouuseanumberthatislargerthan15?
Howdoyougetthecolorsbacktonormal(whitescreenandblackpen)?

Red,Green,andBlue
Whatdowedoifwewanttouseacolorthatisnotinthecolortable?Wecanspecifyitusingthecolorsred,
green,andbluecomponents.Thisissometimescalledan"RGBvalue".
Everycoloristheworldismadeupofred,green,andblue.Takentogether,red,green,andbluearecalled
"theprimarycolors"becauseyoucancreateeverycolorjustbymixingred,green,andbluetogether.
Youmaybethinking"wait,Ithoughttheprimarycolorswerered,yellowandblue."Well,that'spartlytrue.
Thoseareactuallythe"primarypigmentcolors".Theyaretheprimarycolorswhendealingwithpaint.
Actually,theprimarypigmentcolorsaren'texactlyred,yellow,andbluethey'remagenta(areddishpurple),
yellow,andcyan(alightblue).Butwhenyou'redealingwithlight,theprimarycolorsarered,green,andblue.
Thereasonwhyred,green,andbluearesoimportanthasnothingtodowithphysicsorcomputers.Ithastodo
withthestructureofthehumaneye.Thehumaneyecontainsthreetypesofcolorreceptorscalled"cones".
Onetypeofconeissensitivetoredlight,anotherissensitivetogreenlight,andanotherissensitivetoblue
light.Thebrainmixesthestimulusfromeachofthesetypesofconesandcreatestheperceptionofcolors.
That'sallveryinteresting,butwhatdoesthishavetodowithLogo?Well,Logoletsyouspecifycolorsin
termsofred,green,andblue.ByusingRGBvalues,youcanpickanyofthecolorsinthetableaboveand
morethat16millionothers.Insteadofspecifyinganumberfrom015,youspecifyatripleofnumbers,like
this"SETPENCOLOR[187187187]".
Activity:
Let'swriteaprogramthatdrawsabluesquareonayellowbackground.Butthistime,let'suseRGB
values,insteadofacolorindex.

SETSCREENCOLOR[2552550]
SETPENCOLOR[00255]
REPEAT4[FORWARD100RIGHT90]

Trychangingthenumbersinthesquarebracketandseewhathappens.

Activity:ExperimentwithSETPENCOLORandSETSCREENCOLORtocreateyourowninteresting
pictures.YoucanstartwithanyprogramfromtheotherlessonsandinsertaSETSCREENCOLOR
commandnearthebeginning,orinsertaSETPENCOLORcommandjustbeforetheturtlemoves.

SamplePrograms
TOSQUARE
REPEAT4[FORWARD50RIGHT90]
END
TOSETPEN:BRIGHTNESS
SETPENCOLOR(LIST
255;red
255:BRIGHTNESS;green
:BRIGHTNESS;blue
)
END
TOSQUAREFLOWER
REPEAT64[
SETPENREPCOUNT*41
RIGHT360/64
SQUARE
]
END
SQUAREFLOWER

TOCOLORSTAR
SETSCREENCOLOR0
REPEAT35[
SETPENCOLORINT(REPCOUNT1)/5
FORWARDREPCOUNT*6
RIGHT144
]
END
COLORSTAR

TOCOLORBALL
REPEAT4[
SETPENCOLORREPCOUNT
REPEAT120[FORWARDREPCOUNT*2RIGHT204]
HOME
RIGHT90*REPCOUNT
]
END
COLORBALL

TORECTANGLE:HEIGHT:WIDTH
REPEAT2[
FORWARD:HEIGHT
RIGHT90
FORWARD:WIDTH
RIGHT90
]
END
TOTRIANGLE:LENGTH
RIGHT45
FORWARD:LENGTH*(SQRT2)/2
RIGHT90
FORWARD:LENGTH*(SQRT2)/2
RIGHT135
FORWARD:LENGTH
RIGHT90
END
TOHOUSE
;drawaredroof
SETPENCOLOR[25500]
FORWARD100
TRIANGLE100
BACK100
;drawadarkgreendoor
SETPENCOLOR[10016264]
RIGHT90

FORWARD60
LEFT180
RECTANGLE2040
FORWARD60
RIGHT90
;drawabrownhouse
SETPENCOLOR[1559659]
RECTANGLE100100
END
HOUSE

Vous aimerez peut-être aussi