How to make glitter paint

General FreeBASIC programming questions.
Post Reply
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

How to make glitter paint

Post by BasicCoder2 »

Does anyone know how to generate glitter paint from some base color?
My assumption here is a routine that instead of using a palette of colors you can generate a random colors from a base color.

Code: Select all

screenres 640,480,32

dim as ulong colors1(54)
dim as ulong colors2(54)  'to make more sets of colors
dim as ulong colors3(54)
dim as ulong colors4(54)

colors1(0)=RGB(255,188,224)
colors1(1)=RGB(226,72,166)
colors1(2)=RGB(211,57,151)
colors1(3)=RGB(250,96,190)
colors1(4)=RGB(255,136,230)
colors1(5)=RGB(124,0,64)
colors1(6)=RGB(218,64,158)
colors1(7)=RGB(196,42,136)
colors1(8)=RGB(189,35,129)
colors1(9)=RGB(205,51,145)
colors1(10)=RGB(234,80,174)
colors1(11)=RGB(253,120,215)
colors1(12)=RGB(186,32,126)
colors1(13)=RGB(173,19,113)
colors1(14)=RGB(254,104,198)
colors1(15)=RGB(255,112,206)
colors1(16)=RGB(180,26,120)
colors1(17)=RGB(255,166,254)
colors1(18)=RGB(237,83,177)
colors1(19)=RGB(170,16,110)
colors1(20)=RGB(255,127,224)
colors1(21)=RGB(244,90,184)
colors1(22)=RGB(164,10,104)
colors1(23)=RGB(157,3,97)
colors1(24)=RGB(149,0,89)
colors1(25)=RGB(255,128,222)
colors1(26)=RGB(255,214,255)
colors1(27)=RGB(107,0,35)
colors1(28)=RGB(202,48,142)
colors1(29)=RGB(255,151,246)
colors1(30)=RGB(191,63,160)
colors1(31)=RGB(109,46,150)
colors1(32)=RGB(255,144,238)
colors1(33)=RGB(232,104,201)
colors1(34)=RGB(155,92,196)
colors1(35)=RGB(181,53,150)
colors1(36)=RGB(170,107,211)
colors1(37)=RGB(166,38,135)
colors1(38)=RGB(135,0,75)
colors1(39)=RGB(118,0,87)
colors1(40)=RGB(159,96,200)
colors1(41)=RGB(202,139,243)
colors1(42)=RGB(154,26,123)
colors1(43)=RGB(134,6,103)
colors1(44)=RGB(187,124,228)
colors1(45)=RGB(255,253,255)
colors1(46)=RGB(133,26,126)
colors1(47)=RGB(255,198,248)
colors1(48)=RGB(141,0,81)
colors1(49)=RGB(214,73,169)
colors1(50)=RGB(139,76,180)
colors1(51)=RGB(125,62,166)
colors1(52)=RGB(109,0,49)
colors1(53)=RGB(255,231,255)

'make second set from first set
dim as ulong v,r,g,b
for i as integer = 0 to 53
    v = colors1(i)
    r = v shr 16 and 255
    g = v shr 8 and 255
    b = v and 255
    colors2(i) = rgb(g,b,r)  '
    colors3(i) = rgb(b,r,g)
    colors4(i) = rgb(r,b,g)
next i

dim shared as any ptr canvas(0 to 2)
for k as integer = 0 to 2
    canvas(k) = imagecreate(640,480,rgb(255,0,255))
next k

dim as integer mx,my,mb
dim as integer frame,count,paintID
paintID = 0
dim as string key

do
    
    key = inkey
    if key = "1" then
        paintID = 0
    elseif key = "2" then
        paintID = 1
    elseif key = "3" then
        paintID = 2
    elseif key = "4" then
        paintID = 3
    end if
        
    while inkey<>"":wend

    
    getmouse mx,my,,mb
    
    if mb = 1 then
        for j as integer = -3 to 3
            for i as integer = -3 to 3
                if paintID = 0 then
                    pset canvas(0),(mx+i,my+j),colors1(int(Rnd(1)*54))
                    pset canvas(1),(mx+i,my+j),colors1(int(rnd(1)*54))
                    pset canvas(2),(mx+i,my+j),colors1(int(rnd(1)*54))
                elseif paintID = 1 then
                    pset canvas(0),(mx+i,my+j),colors2(int(Rnd(1)*54))
                    pset canvas(1),(mx+i,my+j),colors2(int(rnd(1)*54))
                    pset canvas(2),(mx+i,my+j),colors2(int(rnd(1)*54))
                elseif paintID = 2 then
                    pset canvas(0),(mx+i,my+j),colors3(int(Rnd(1)*54))
                    pset canvas(1),(mx+i,my+j),colors3(int(rnd(1)*54))
                    pset canvas(2),(mx+i,my+j),colors3(int(rnd(1)*54))
                elseif paintID = 3 then
                    pset canvas(0),(mx+i,my+j),colors4(int(Rnd(1)*54))
                    pset canvas(1),(mx+i,my+j),colors4(int(rnd(1)*54))
                    pset canvas(2),(mx+i,my+j),colors4(int(rnd(1)*54))
                end if
                    
            next i
        next j
    end if

    screenlock
    cls
    put (0,0),canvas(frame),trans
    locate 2,2
    print "PRESS KEY [1] or [2] or [3] or [4] to select color"
    print
    print " USE MOUSE TO DRAW"
    screenunlock
    
    count = count + 1
    if count > 20 then
        count = 0
    else
        count = count + 1
        frame = frame + 1
        if frame = 3 then frame = 0
    end if

    
    sleep 2
    
loop until multikey(&H01)

Last edited by BasicCoder2 on Oct 13, 2018 11:04, edited 3 times in total.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: HOW TO MAKE GLITTER PAINT?

Post by badidea »

Trying to paint my horse?
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

re: How to make glitter paint

Post by BasicCoder2 »

badidea wrote:Trying to paint my horse?
Yes it was the horse that made me think about adding glitter paint to a Paint program.
I have added some more colors to the program in the first post.
Not sure how to convert this to a .gif image and thus have my own version using 3 frames.
Actually I think a .gif file is compressed and only changes between frames need be saved.
To make it a complete paint program I would have to add the option to turn a base color into glitter and the option to fill or paint with it.
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: How to make glitter paint

Post by dafhi »

I would make a buffer of pointers as big as your image

1. each pointer 'pixel' refers to the address of an actual pixel, or the address of a palette color
2. the palette can have individual cycles.

this guy gives a great talk .. https://www.youtube.com/watch?v=aMcJ1Jvtef0
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: How to make glitter paint

Post by BasicCoder2 »

@dafhi,
Ultimately I would imagine artwork of this kind being saved as a well supported .gif file.
Even color cycling would require working out what range of palette colors to cycle and what values to give them for any given application.
It is usual I think to just have a short animated tile for water effects or as in my example one big animated tile the size of the display.
I assume you mean something like this,

Code: Select all

'palette
dim shared as integer qbColor(0 to 15)
qbColor(0)  = &H000000
qbColor(1)  = &H0000AA
qbColor(2)  = &H00AA00
qbColor(3)  = &H00AAAA
qbColor(4)  = &HAA0000
qbColor(5)  = &HAA00AA
qbColor(6)  = &HAA5500
qbColor(7)  = &HAAAAAA
qbColor(8)  = &H555555
qbColor(9)  = &H5555FF
qbColor(10) = &H55FF55
qbColor(11) = &H55FFFF
qbColor(12) = &HFF5555
qbColor(13) = &HFF55FF
qbColor(14) = &HFFFF55
qbColor(15) = &HFFFFFF

screenres 640,480,32

dim shared as integer pScreen(0 to 639,0 to 479)  'array of palette numbers

sub drawScreen()
    screenlock
    cls
    for j as integer = 0 to 479
        for i as integer = 0 to 639
            'for speed probably place color directly in a 640x480 bitmap to PUT on screen
            pset (i,j),qbColor(pScreen(i,j))
        next i
    next j
    screenunlock
end sub

'fill screen with random palette numbers
for j as integer = 0 to 479
    for i as integer = 0 to 639
        pScreen(i,j)= 0
    next i
next j

sub cycleColors()
    dim as ulong temp
    temp = qbColor(0)
    for i as integer = 0 to 15
        qbColor(i)=qbColor(i+1)
    next i
    qbColor(15)=temp
end sub

do
    drawScreen()
    cycleColors()
    locate 2,2
    print "TAP SPACE BAR TO CYCLE"
    sleep
loop until multikey(&H01)
Or using one of the glitter palettes,

Code: Select all

'palette
dim shared as integer colors1(0 to 54)
colors1(0)=RGB(255,188,224)
colors1(1)=RGB(226,72,166)
colors1(2)=RGB(211,57,151)
colors1(3)=RGB(250,96,190)
colors1(4)=RGB(255,136,230)
colors1(5)=RGB(124,0,64)
colors1(6)=RGB(218,64,158)
colors1(7)=RGB(196,42,136)
colors1(8)=RGB(189,35,129)
colors1(9)=RGB(205,51,145)
colors1(10)=RGB(234,80,174)
colors1(11)=RGB(253,120,215)
colors1(12)=RGB(186,32,126)
colors1(13)=RGB(173,19,113)
colors1(14)=RGB(254,104,198)
colors1(15)=RGB(255,112,206)
colors1(16)=RGB(180,26,120)
colors1(17)=RGB(255,166,254)
colors1(18)=RGB(237,83,177)
colors1(19)=RGB(170,16,110)
colors1(20)=RGB(255,127,224)
colors1(21)=RGB(244,90,184)
colors1(22)=RGB(164,10,104)
colors1(23)=RGB(157,3,97)
colors1(24)=RGB(149,0,89)
colors1(25)=RGB(255,128,222)
colors1(26)=RGB(255,214,255)
colors1(27)=RGB(107,0,35)
colors1(28)=RGB(202,48,142)
colors1(29)=RGB(255,151,246)
colors1(30)=RGB(191,63,160)
colors1(31)=RGB(109,46,150)
colors1(32)=RGB(255,144,238)
colors1(33)=RGB(232,104,201)
colors1(34)=RGB(155,92,196)
colors1(35)=RGB(181,53,150)
colors1(36)=RGB(170,107,211)
colors1(37)=RGB(166,38,135)
colors1(38)=RGB(135,0,75)
colors1(39)=RGB(118,0,87)
colors1(40)=RGB(159,96,200)
colors1(41)=RGB(202,139,243)
colors1(42)=RGB(154,26,123)
colors1(43)=RGB(134,6,103)
colors1(44)=RGB(187,124,228)
colors1(45)=RGB(255,253,255)
colors1(46)=RGB(133,26,126)
colors1(47)=RGB(255,198,248)
colors1(48)=RGB(141,0,81)
colors1(49)=RGB(214,73,169)
colors1(50)=RGB(139,76,180)
colors1(51)=RGB(125,62,166)
colors1(52)=RGB(109,0,49)
colors1(53)=RGB(255,231,255)

screenres 640,480,32

dim shared as integer pScreen(0 to 639,0 to 479)  'array of palette numbers

sub drawScreen()
    screenlock
    cls
    for j as integer = 0 to 479
        for i as integer = 0 to 639
            'for speed probably place color directly in a 640x480 bitmap to PUT on screen
            pset (i,j),colors1(pScreen(i,j))
        next i
    next j
    screenunlock
end sub

'fill screen with random palette numbers
for j as integer = 0 to 479
    for i as integer = 0 to 639
        pScreen(i,j)= int(Rnd(1)*55)
    next i
next j

sub cycleColors()
    dim as ulong temp
    temp = colors1(0)
    for i as integer = 0 to 54
        colors1(i)=colors1(i+1)
    next i
    colors1(54)=temp
end sub

do
    drawScreen()
    cycleColors()
    sleep 30
loop until multikey(&H01)

Last edited by BasicCoder2 on Oct 13, 2018 20:25, edited 1 time in total.
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: How to make glitter paint

Post by dafhi »

I assumed you were attempting a paint program which uh .. then would create an animated .. yeah i guess .gif would be the end result.

It's an easy enough scenario and interesting, who knows I might have a crack at it
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: How to make glitter paint

Post by BasicCoder2 »

The color range was taken from the Pony image and I inadvertently took two sets (reddish and a bluish set) together.
My problem is how to work out a function that given a base color will return a random color suitable to fill an area with glitter based on that base color.
For example I think the base color for the example set below was probably derived from a base color close to this,
rgb(205,37,98)
So how to generate a set of colors like this (taken from an area on the Pony image).

Code: Select all

screenres 800,480,32

dim as ulong colors(0 to 41)
colors(0)=RGB(205,37,98)
colors(1)=RGB(243,75,136)
colors(2)=RGB(173,5,66)
colors(3)=RGB(140,0,33)
colors(4)=RGB(201,33,94)
colors(5)=RGB(165,0,58)
colors(6)=RGB(216,48,109)
colors(7)=RGB(117,0,10)
colors(8)=RGB(56,0,0)
colors(9)=RGB(236,68,129)
colors(10)=RGB(255,120,181)
colors(11)=RGB(253,134,183)
colors(12)=RGB(211,43,104)
colors(13)=RGB(85,0,6)
colors(14)=RGB(233,65,126)
colors(15)=RGB(179,11,72)
colors(16)=RGB(104,0,0)
colors(17)=RGB(124,0,17)
colors(18)=RGB(227,59,120)
colors(19)=RGB(133,0,26)
colors(20)=RGB(149,0,42)
colors(21)=RGB(255,104,165)
colors(22)=RGB(184,16,77)
colors(23)=RGB(195,27,88)
colors(24)=RGB(255,96,157)
colors(25)=RGB(255,136,197)
colors(26)=RGB(220,52,113)
colors(27)=RGB(189,21,82)
colors(28)=RGB(157,0,50)
colors(29)=RGB(249,81,142)
colors(30)=RGB(255,153,214)
colors(31)=RGB(254,184,247)
colors(32)=RGB(255,198,248)
colors(33)=RGB(23,0,0)
colors(34)=RGB(39,0,0)
colors(35)=RGB(254,88,149)
colors(36)=RGB(255,214,255)
colors(37)=RGB(255,168,229)
colors(38)=RGB(255,150,199)
colors(39)=RGB(72,0,1)
colors(40)=RGB(255,231,255)
colors(41)=RGB(163,43,91)

for i as integer = 0 to 41
    line (i*16,0)-(i*16+15,16),colors(i),bf
next i

sleep
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: How to make glitter paint

Post by dafhi »

how about something like

Code: Select all

type ByteRange
   declare operator cast as ubyte
   declare operator cast as string
   
   as integer bas = 0
   as integer range = 256
end type

operator ByteRange.cast as ubyte
   return rnd * range - .5 + bas
end operator

operator ByteRange.cast as string
   return str(cubyte(this))
end operator


dim as ByteRange red

for i as long = 1 to 10
 ? red
next
Last edited by dafhi on Oct 14, 2018 6:49, edited 1 time in total.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: How to make glitter paint

Post by BasicCoder2 »

Sorting a sample I see the algorithm used produces the full brightness range of a base color.
So the algorithm needed will return a random brightness value of a base color.

Code: Select all

screenres 640,480,32

dim as ulong colors(0 to 38)
colors(0)=RGB(171,51,99)
colors(1)=RGB(107,0,35)
colors(2)=RGB(255,150,199)
colors(3)=RGB(163,43,91)
colors(4)=RGB(139,19,67)
colors(5)=RGB(195,75,123)
colors(6)=RGB(203,83,131)
colors(7)=RGB(255,168,229)
colors(8)=RGB(253,134,183)
colors(9)=RGB(219,99,147)
colors(10)=RGB(255,136,197)
colors(11)=RGB(211,91,139)
colors(12)=RGB(187,67,115)
colors(13)=RGB(235,115,163)
colors(14)=RGB(99,0,27)
colors(15)=RGB(155,35,83)
colors(16)=RGB(254,184,247)
colors(17)=RGB(179,59,107)
colors(18)=RGB(255,166,215)
colors(19)=RGB(131,11,59)
colors(20)=RGB(255,198,248)
colors(21)=RGB(56,0,0)
colors(22)=RGB(227,107,155)
colors(23)=RGB(123,3,51)
colors(24)=RGB(0,0,0)
colors(25)=RGB(72,0,1)
colors(26)=RGB(245,121,171)
colors(27)=RGB(147,27,75)
colors(28)=RGB(115,0,43)
colors(29)=RGB(23,0,0)
colors(30)=RGB(255,188,224)
colors(31)=RGB(39,0,0)
colors(32)=RGB(255,214,255)
colors(33)=RGB(85,0,6)
colors(34)=RGB(255,153,214)
colors(35)=RGB(91,0,19)
colors(36)=RGB(232,116,157)
colors(37)=RGB(255,253,255)
colors(38)=RGB(255,231,255)

for i as integer = 0 to 38
    line (i*16,0)-(i*16+15,16),colors(i),bf
next i

'sort by brightness
dim as ulong v1,v2,r1,r2,g1,g2,b1,b2
for j as integer = 0 to 38
    for i as integer = 0 to 38
        
        v1 = colors(i)
        r1 = v1 shr 16 and 255
        g1 = v1 shr 8 and 255
        b1 = v1 and 255
        v1 = (r1+g1+b1)\3
        
        v2 = colors(i+1)
        r2 = v2 shr 16 and 255
        g2 = v2 shr 8 and 255
        b2 = v2 and 255
        v2 = (r2+g2+b2)\3
        
        if v1>v2 then
            swap colors(i),colors(i+1)
        end if
    next i
next j

for i as integer = 0 to 38
    line (i*16,16)-(i*16+15,32),colors(i),bf
next i

sleep
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: How to make glitter paint

Post by dafhi »

rnd * range - .5 + bas will give a balanced distrib. I shoulda used those instead. You can mod if u want. Otherwise i'll make the change tomm.

- updated
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: How to make glitter paint

Post by sancho3 »

GIF used to be copy protected format that required payment to include in your program. The payment aspect ended (check this for yourself as I am not at all certain) although the copyright is still claimed. (To the best of my knowledge)
Quoted from wikipedia: "By 2004 all the relevant patents had expired".
The .GIF file format specification is broken down for you at the wikipedia site.
I would call rolling your own GIF code quite a challenge although certainly doable.
Have you searched for pre-existing libraries that might speed the implementation?
Post Reply