DRAW DASH Colored lines

Post Reply
User avatar
rbytes
Posts: 1794
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

DRAW DASH Colored lines

Post by rbytes » Mon Oct 29, 2018 4:51 am

I have been thinking about new ways to draw lines. It popped into my head this week that it would be easy to create segmented lines using the DRAW DASH command. By changing the DRAW DASH parameters and drawing multiple lines at the same location, I could create continuous lines with fixed or moving color cycles.

Code: Select all

/*
Dash Line Demo by rbytes
October, 2018
How rainbow-colored lines are made.
To create one line with an 8-colors repeating pattern, you have to draw 8 dashed lines! Each line is preset with DRAW DASH so that it draws segments of one color and leaves a 7-segment gap between them. The segments of each line are offset from the other lines. When all 8 dashed lines are drawn on top of each other, what you see is a rainbow line.

In this demo, the 8 dashed lines are first drawn on top of each other, creating a rainbow line with 8 colors. Then the dashed lines are drawn separately beneath, so that you can see their visible segments, and understand how the 8 lines merge to form one.

Slowly the segment length is increased for all of the dashed lines.
*/
option base 1
set toolbar off
get screen size sw,sh
graphics
graphics clear 0,0,0
draw alpha 1
ds=20
inc=1
draw size ds
cshift=0
mult=1
mult2=mult*7
timer=TIME()
tmmy=0
draw color 0,1,1
fill color 0,0,0
draw font size 22
draw font name "Arial"
a$="To create one line with 8 colors, you have to draw 8 dashed lines! Each dashed line draws segments of just one color, leaving 7-segment-wide blank spaces between them. The segments of each dashed line are offset horizontally from those of the previous line by a one-segment width. When all the dashed lines are drawn on top of each other, what you see are two rainbow lines, as shown at the top."
b$="Every second, the length of each segment is increased by 1. The seven dashed lines are redefined to use the new segment size. The dashed lines are redrawn on top of each other to create the two rainbow lines, and then they are drawn separately below them. All the lines in this demo have a DRAW SIZE of 20 (20 points width) except the top line, which is drawn at DRAW SIZE 1 (1 point size)."
FIELD "1" TEXT " "&mult AT sw*.47,sh*.42 SIZE 50,40 RO
FIELD "info1" TEXT a$ at sw*.05,sh*.46 SIZE sw/3,sh/3.6 ML RO
FIELD "info2" TEXT b$ at sw*.6,sh*.46 SIZE sw/3,sh/3.6 ML RO
draw text "SEGMENT LENGTH" at sw*.4,sh*.36

loop:
for phs=0 to 7
  DRAW DASH mult,mult2 PHASE mult*phs   ' set the dash pattern
  pal(cshift+phs/8,1,1,1,.5)            ' set the color
  draw color pal.r,pal.g,pal.b
  gosub liner
next phs

' touch (hold) to end

GET TOUCH 0 AS tx,ty
IF tx<>-1 THEN
  IF laun$="desktop" THEN
    IF FILE_EXISTS("/launch") THEN
      RUN "/-Launch.sb"
    ELSE
      EXIT
    ENDIF
  ENDIF
  END
ENDIF

mult+=inc
mult2=mult*7
if mult>370 then inc=-1
if mult<2 then inc=1
tmmy = INT(TIME()-timer)
FIELD "1" TEXT " "&mult
pause 1
goto loop
end

liner:
draw line 0,50 to sw,50     ' multicolored line

if phs=0 then               ' red line
  fill rect 0,60 to sw,80
  draw line 0,70 to sw,70
endif
if phs=1 then               ' orange line
  fill rect 0,80 to sw,100
  draw line 0,90 to sw,90
endif
if phs=2 then               ' yellow-green line
  fill rect 0,100 to sw,120
  draw line 0,110 to sw,110
endif
if phs=3 then               ' green line
  fill rect 0,120 to sw,140
  draw line 0,130 to sw,130
endif
if phs=4 then               ' cyan line
  fill rect 0,140 to sw,160
  draw line 0,150 to sw,150
endif
if phs=5 then               ' blue line
  fill rect 0,160 to sw,180
  draw line 0,170 to sw,170
endif
if phs=6 then               ' magenta line
  fill rect 0,180 to sw,200
  draw line 0,190 to sw,190
endif
if phs=7 then               ' rose line
  fill rect 0,200 to sw,220
  draw line 0,210 to sw,210
endif
draw size 1
draw line 0,30 to sw,30     ' multicolored line
draw size ds
return


'r' ===== Combined Palette function based on Henko and Dutchman converters, by rbytes
DEF Pal(Tint,SATURATION,Brighten,Intensity,Gamma)
' ---- adjust parameter-values between zero and 1.
h=tint*360
tint%=1
Sat=ABS(SATURATION)
IF Sat>1 THEN Sat%=1
Intensity=ABS(Intensity) 
IF Intensity>1 THEN Intensity%=1
Brighten=ABS(Brighten)
IF Brighten>1 THEN Brighten%=1
r=0 ! g=0 ! b=0 ! h%=360
IF h<120 OR h>240 THEN r=palsub(ABS(h-360*FLOOR(h/240)),Gamma)
IF h<240 THEN g=palsub(ABS(h-120),Gamma)
IF h>120 THEN b=palsub(ABS(h-240),Gamma)
Sat: ' with constant luminance
' ---- apply Saturation and Brighten
IF Sat<1 THEN
  Y=0.3*R+0.59*G+0.11*B
  U=B-Y ! U*=sat
  V=R-Y ! v*=sat
  W=G-Y ! w*=sat
  R=Y+V ! G=Y+W ! B=Y+U
  IF Brighten<>0 THEN
    Ysup=1-MAX(MAX(R,G),B)' max white to add
    Ysup*=Brighten
    R+=Ysup ! G+=Ysup ! B+=Ysup
  ENDIF
ENDIF
' ---- apply Intensity
IF Intensity<1 THEN
  R*=Intensity
  G*=Intensity
  B*=Intensity
ENDIF
Y=0.3*R+0.59*G+0.11*B
END DEF ' Pal
DEF palsub(e,Gamma)
f=Gamma   ' 0<=f<=1 better balance between primary and secondary colors
IF e<60 THEN c=1 ELSE ! x=(120-e)/60 ! c=x*(1+f-f*x) ! END IF
RETURN c
END DEF
Attachments
401122E9-9BAC-42C1-BB11-3EFA36A1A7CF.png
401122E9-9BAC-42C1-BB11-3EFA36A1A7CF.png (195.06 KiB) Viewed 1243 times
####### Living the colorful life #######

Post Reply