Pixel

New to FreeBASIC? Post your questions here.
TurtleProgrammer
Posts: 37
Joined: Jan 26, 2017 7:54

Pixel

Post by TurtleProgrammer »

What's the easiest way to create a randomly colored pixel from 1 to 1001 for example?
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Pixel

Post by vdecampo »

TurtleProgrammer wrote:What's the easiest way to create a randomly colored pixel from 1 to 1001 for example?
is 1-1001 the range of colors or the number of pixels?

-Vince
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Pixel

Post by vdecampo »

vdecampo wrote:
TurtleProgrammer wrote:What's the easiest way to create a randomly colored pixel from 1 to 1001 for example?
is 1-1001 the range of colors or the number of pixels?

Code: Select all

#Define RndColor	Rnd*&hFFFFFF
#Define ScrW 		800
#Define ScrH		600

ScreenRes ScrW, ScrH

For x As Integer = 0 To 1000
	PSet (Rnd*ScrW,Rnd*ScrH),RndColor
Next

Sleep
-Vince
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Pixel

Post by vdecampo »

vdecampo wrote:
vdecampo wrote:
TurtleProgrammer wrote:What's the easiest way to create a randomly colored pixel from 1 to 1001 for example?
is 1-1001 the range of colors or the number of pixels?

Code: Select all

#Define RndColor	Rnd*&hFFFFFF
#Define ScrW 		800
#Define ScrH		600

Randomize
ScreenRes ScrW, ScrH

For x As Integer = 0 To 1000
	PSet (Rnd*ScrW,Rnd*ScrH),RndColor
Next

Sleep
-Vince
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Pixel

Post by MrSwiss »

Color Ranges (commonly used):
8 bit (1 Byte) = 0 to 255 (aka: Color-Palette)
32 bit (4 Byte) = 0 to &hFFFFFFFF (a ULong containing: 4 UBytes referred to as color channels)

Color Channels are:
b = blue
g = green
r = red
a = Alpha (see-through to opaque)
each of which, has a range of: 0 - 255 (&h00 - &hFF)
TurtleProgrammer
Posts: 37
Joined: Jan 26, 2017 7:54

Re: Pixel

Post by TurtleProgrammer »

To clarify more clearly, I am writing a program where

x = 1001 pixels per line * y = 1001 lines

Then each pixel is a random color
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Pixel

Post by dodicat »

You will require a screen size 1001 by 1001, and 32 bits depth.
Just loop through every pixel, painting each one a random colour.
Saying that, 1001 picture height is big, many monitors (including this one) are not set to such a big vertical resolution.

Code: Select all


screenres 1001,1001,32
for x as long=0 to 1000
    for y as long=0 to 1000
        pset(x,y),rgb(rnd*255,rnd*255,rnd*255)
    next y
next x

sleep 
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Pixel

Post by MrSwiss »

You can also use a single line Macro called #Define to obtain a 32bit random color.
Without the use of the RGB/RGBA Macro's:

Code: Select all

#Define RndColor32()    ( CULng(Rnd() * &hFFFFFF + &hFF000000) )  ' Alpha = 255, always

ScreenRes(1001, 1001, 32)

For j As UInteger = 0 To 1000       ' vertical
    For i As UInteger = 0 To 1000   ' horizontal 
        PSet (i, j), RndColor32()
    Next
Next

Sleep
TurtleProgrammer
Posts: 37
Joined: Jan 26, 2017 7:54

Re: Pixel

Post by TurtleProgrammer »

I need the program to start from 1 to and go to 1000 (for example), and pick a color from the standard, red, blue, green, yellow, orange, and white.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Pixel

Post by vdecampo »

TurtleProgrammer wrote:I need the program to start from 1 to and go to 1000 (for example), and pick a color from the standard, red, blue, green, yellow, orange, and white.
Which graphics mode? 32bit or 8bit?

-Vince
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Pixel

Post by dodicat »

For six colours only, set them in an array (ulong here--32 bit).
Then pick any one of the six colours randomly (via the function RANGE), and colour your pixels that way.

Code: Select all

function range(f as long,l as long) as long
    return int(Rnd*((l+1)-f)+f)
 end function
 
 
 dim as ulong a(1 to 6)
 a(1)=rgb(255,0,0)    'red
 a(2)=rgb(0,0,255)    'blue
 a(3)=rgb(0,255,0)    'green
 a(4)=rgb(255,255,0)  'yellow
 a(5)=rgb(255,100,0)  'orange
 a(6)=rgb(255,255,255)'white
 
 
 screenres 1001,1001,32

for x as long=0 to 1000
    for y as long=0 to 1000
        pset(x,y),a(range(1,6))
    next y
next x

sleep
 
  
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Pixel

Post by MrSwiss »

TurtleProgrammer wrote:I need the program to start from 1 to and go to 1000 (for example)
This does not work well, because pixel addressing starts (in graphics mode) by: x = 0, y = 0
(otherwise you'll have to correct for that, later on in the loop's):

Code: Select all

for y as uinteger = 1 to 1001
    for x as uinteger = 1 to 1001
        pset (x-1, y-1), ...
    next
next
This is uneconomical, as well as a speed-brake.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Pixel

Post by BasicCoder2 »

TurtleProgrammer wrote:
What's the easiest way to create a randomly colored pixel from 1 to 1001 for example?
I need the program to start from 1 to and go to 1000 (for example), and pick a color from the standard, red, blue, green, yellow, orange, and white.
TurtleProgrammer's question has changed and it would be helpful if the desired use of these 1000 pixels was explained. The need to start with 1 may not be required but we wouldn't know without knowing what program using these 1000 pixels is supposed to do.
.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Pixel

Post by dodicat »

For the 8 bit screen these seem to be the six colours required:

Code: Select all

function range(f as long,l as long) as long
    return int(Rnd*((l+1)-f)+f)
 end function
 
 
 dim as ulong a(1 to 6)
 a(1)=4          'rgb(170,0,0)        'red
 a(2)=1          'rgb(0,0,170)        'blue
 a(3)=10         'rgb(85,255,85)      'green
 a(4)=14         'rgb(255,255,85)     'yellow
 a(5)=12         'rgb(255,85,85)      'orange
 a(6)=15         'rgb(255,255,255)    'white
 
 
 screenres 1001,1001

for x as long=0 to 1000
    for y as long=0 to 1000
        pset(x,y),a(range(1,6))
    next y
next x

sleep
 
  
I have kept the array as ulong, although for 8 bit ubyte would do.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: Pixel

Post by dafhi »

Code: Select all

'palette demo

ScreenRes 640, 480, 8  '8 bits per pixel

dim as long  BigPixel = 16
dim as long  m = BigPixel - 1

for yy as long = 0 to 15
  for xx as long = 0 to 15
    dim as long c = yy * 16 + xx
    dim as long x = xx*BigPixel, y = yy*BigPixel
    line (x,y)-(x+m,y+m), c, bf 'bar filled
  next
next

sleep
freebasic documentation
Post Reply