Need an algorithm

General FreeBASIC programming questions.
Post Reply
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Need an algorithm

Post by vdecampo »

I have looked this color chart over and over I cannot figure out an algorithm for generating it manually. Can anybody help me with this?

Image

Thanks
-Vince
bfuller
Posts: 362
Joined: Jun 02, 2007 12:35
Location: Sydney, Australia

Post by bfuller »

If you open MS Paint, in the Start, Accessories options, and on the top menu bar, you can go to Colors, Edit Colors then select define custom colors. When you have this custom color window open, you can see the R,G,B and Hue,Sat,Lum of every color as you drag the cross hairs and slider bar on the right around. From this you should be able to work it all out.

Good luck.

Happy New Year.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

As I said, Ive already looked this image over. I used Photoshop. I was hoping someone had already produced this type of image in code and would share it. Thanks anyway.

-Vince
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

I don't know if it helps, but the color channels of that specific image all look pretty much like this, give or take 120 degrees. This is the red one:
Image
Mysoft
Posts: 836
Joined: Jul 28, 2005 13:56
Location: Brazil, Santa Catarina, Indaial (ouch!)
Contact:

Post by Mysoft »

i remember that i did that before... now its probabily a little different
maybe there's more a formal algorithm for that... (wikipedia have lot info about colors), but in my style of "discovering" here it is:

Code: Select all

#define Fraction(A,B,P,M) (A+(((B-A)/M)*P))

type RGBstruct
  R as short
  G as short
  B as short
end type

dim as RGBstruct HUE(6) = _
{(255,0,0),(255,255,0),(0,255,0),(0,255,255),(0,0,255),(255,0,255),(255,0,0)}
dim as RGBstruct HUETABLE(239)

' Creating HUE color table
scope
  dim as integer CNT,HUEIN,HUEOUT=0
  dim as integer RA,GA,BA,RB,GB,BB
  for HUEIN = 0 to 5
    RA=HUE(HUEIN).R:GA=HUE(HUEIN).G: BA=HUE(HUEIN).B
    RB=HUE(HUEIN+1).R:GB=HUE(HUEIN+1).G: BB=HUE(HUEIN+1).B
    for CNT = 0 to 39      
      with HUETABLE(HUEOUT)
        .R = Fraction(RA,RB,CNT,40)
        .G = Fraction(GA,GB,CNT,40)
        .B = Fraction(BA,BB,CNT,40)
      end with
      HUEOUT += 1
    next CNT
  next HUEIN
end scope

dim as integer RR,GG,BB,TON

screenres 320,240,32

Windowtitle "HUE/Luminance"
for XX as integer = 0 to 239
  with HUETABLE(XX)
    for YY as integer = -100 to 100
      if YY < 0 then TON = 255 else TON=0
      RR = Fraction(.R,TON,abs(YY),100)
      GG = Fraction(.G,TON,abs(YY),100)
      BB = Fraction(.B,TON,abs(YY),100)      
      pset(XX+40,120+YY),rgb(RR,GG,BB)
    next YY    
  end with
next XX
line(39,19)-(280,221),rgb(255,255,255),b,&hF0F0
line(39,19)-(280,221),rgb(0,0,0),b,&h0F0F
sleep

Windowtitle "HUE/Saturation"
for XX as integer = 0 to 239
  with HUETABLE(XX)
    for YY as integer = 0 to 200    
      RR = Fraction(.R,128,YY,200)
      GG = Fraction(.G,128,YY,200)
      BB = Fraction(.B,128,YY,200)      
      pset(XX+40,20+YY),rgb(RR,GG,BB)
    next YY    
  end with
next XX
line(39,19)-(280,221),rgb(255,255,255),b,&hF0F0
line(39,19)-(280,221),rgb(0,0,0),b,&h0F0F
sleep
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

Thanks for the responses!

@Mysoft

Thanks for that example I will probably use a variant of your code.

-Vince
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

RGB scan

Post by kiyotewolf »

That code is a two dimensional function, Hue & Intensity. Luminance (color to gray value), is at 100%.

If you use HSVtoRGB color routines, you can use two FOR to NEXT loops to draw that display picture.

If I wasn't so tired, I'd look up the link for you but I'm dead on my laptop keys..

Kiyote!
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

gave in.. rather quickly

Post by kiyotewolf »

I gave in and did it.

http://www.mandelbrot-dazibao.com/Main/Main.htm

Read, learn, enjoy.

Have a donut on me.


Kiyote!
BasicScience
Posts: 489
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Post by BasicScience »

@ kiyotewolf
Thanks for the fantastic link. Great extension to QB4.5 that allows 24-bit color, and some of the best Mandlebrot / Choas examples I've come across.

I became intrigued about color rendering and HSV / HSL / HSB vs RGB. While the "H" systems are more intuitive for human perception, I was at first a bit confused that when Value or Level go to the max of 100% then color information is lost, as the pixel becomes white. (Photoshop appears to use a weird variant of HSB wherein 100% brightness is the 50% Value or Level (colors most vivid).

For the heck of it, I wrote HSV_to_RGB and HSL_To-RGB converters for FB. The sample program also shows differences between the HSV adn HSL systems.

Code: Select all

' HSV to RGB function
' Assumes H (0-360 degrees), S (0 to 100), V (0 to 100)
' Returns RGB (0 - 255)
'
Function HSVtoRGB (byval Hue as single, byval Sat as single, byval Value as single)as uinteger
DIM as single Angle, pi = 3.14159, Ur, Radius, Vr, Wr, Rdim
DIM as single Red, Green, Blue
Angle = (Hue - 150) * Pi / 180
Ur = Value * 2.55
Radius = Ur * TAN(Sat / 84.51663 )     '84.51663 = 100/ATN(SQR(6))

Vr = Radius * COS(Angle) / SQR(2)
Wr = Radius * SIN(Angle) / SQR(6)

Red = Ur - Vr - Wr
Green = Ur + Vr - Wr
Blue = Ur + Wr + Wr

IF Red < 0 THEN
    Rdim = Ur / (Vr + Wr)
    Red = 0
    Green = Ur + (Vr - Wr) * Rdim
    Blue = Ur + 2 * Wr * Rdim
    GOTO Ctrl255
END IF

IF Green < 0 THEN
    Rdim = -Ur / (Vr - Wr)
    Red = Ur - (Vr + Wr) * Rdim
    Green = 0
    Blue = Ur + 2 * Wr * Rdim
    GOTO Ctrl255
END IF

IF Blue < 0 THEN
    Rdim = -Ur / (Wr + Wr)
    Red = Ur - (Vr + Wr) * Rdim
    Green = Ur + (Vr - Wr) * Rdim
    Blue = 0
    GOTO Ctrl255
END IF

Ctrl255:
IF Red   > 255 THEN
    Rdim = (Ur - 255) / (Vr + Wr)
    Red = 255
    Green = Ur + (Vr - Wr) * Rdim
    Blue = Ur + 2 * Wr * Rdim
END IF

IF Green > 255 THEN
    Rdim = (255 - Ur) / (Vr - Wr)
    Red = Ur - (Vr + Wr) * Rdim
    Green = 255
    Blue = Ur + 2 * Wr * Rdim
END IF

IF Blue > 255 THEN
    Rdim = (255 - Ur) / (Wr + Wr)
    Red = Ur - (Vr + Wr) * Rdim
    Green = Ur + (Vr - Wr) * Rdim
    Blue = 255
END IF
Return rgb(cint(Red), cint(Green), cint(Blue))
END Function
'
' HSL to RGB converter
' Assumes H (0-360 degrees), S (0 to 100), L (0 to 100)
' Returns RGB (0 - 255)
'
Function HSLtoRGB (byval H as single, byval s as single, byval l as single) as uinteger
DIM as single Temp1, Temp2, Temp3, Tcolor(0 to 2)
IF S = 0 then
    Return RGB(l,l,l)
end if

IF l < 50 then
    Temp2 = (l/100)*(1 + (s/100))
ELSE
    Temp2 = (L/100) + (S/100) - (L/100)*(S/100)
end if
Temp1 = 2*(L/100) - Temp2

Tcolor(0) = (H/360) + 1/3   'red
Tcolor(1) = H/360           'green
Tcolor(2) = (H/360) - 1/3   'blue

For i as integer = 0 to 2
    IF Tcolor(i) < 0 then Tcolor(i) = Tcolor(i) + 1
    IF Tcolor(i) > 1 then Tcolor(i) = Tcolor(i) - 1
    IF 6 * Tcolor(i) < 1 then
        Tcolor(i) = temp1+(temp2-temp1)*6.0*Tcolor(i)
    ELSEIF 2 * Tcolor(i) < 1 then
        Tcolor(i) = Temp2
    ELSEIF 3 * Tcolor(i) < 2 then
        Tcolor(i) = temp1+(temp2-temp1)*((2.0/3.0)-tcolor(i))*6.0
    ELSE
        Tcolor(i) = Temp1
    end if
next
Return RGB(Tcolor(0)*255, Tcolor(1)*255, Tcolor(2)*255)
end function

'Fucntion to use Multikey to detect keybd event, and importantly to respond
'only once until key is released.
Function KeyPress(Key As Integer) As Integer
Static LastKey(255) As Integer
        If MultiKey(Key) = -1 Then
                If Key = LastKey(Key) Then
                        Return (0)
                Else
                        LastKey(Key)=Key
                        Return (-1)
                Endif
        Else
                LastKey(Key)=0
                Return (0)
        End If
End Function

'
' Main Starts Here
'
Screenres 320,240,32
#include "fbgfx.bi"
using FB
DIM as single H,S,VL
DIM as integer flg, mdl
DIM as uinteger NewColor
line (0,0)-(319,239),rgb(255,255,255),bf
flg = 1 'flag to update screen
mdl = 1 '1 = HSL, 0 = HSV
H = 180
S = 50
VL = 50
DO
IF flg then 
    Line (0,0)-(319,239),rgb(255,255,255), bf
    IF mdl then
        draw string (50, 20), "Model: HSL  F10 / Toggle", rgb(0,0,0)
    ELSE
        draw string (50, 20), "Model: HSV  F10 / Toggle", rgb(0,0,0)
    END IF
    draw string (250, 50), "Up/Dn", rgb(0,0,0)
    draw string (170,70), "Hue: " & H, rgb(0,0,0)
    draw string (250,70), "F1/F2", rgb(0,0,0)
    draw string (170, 82), "Sat: " & S, rgb(0,0,0)
    draw string (250, 82), "F3/F4", rgb(0,0,0)
    IF mdl then
        draw string (170,94), "Lvl: " & VL, rgb(0,0,0)
    ELSE
        draw string (170,94), "Val: " & VL, rgb(0,0,0)
    end if
    draw string (250, 94), "F5/F6", rgb(0,0,0)
    draw string (50, 180), "Escape to quit", rgb(0,0,0)
    IF mdl then
        NewColor = HSLtoRGB(H,S,VL)
    ELSE
        NewColor = HSVtoRGB(H,S,VL)
    END IF
    line (50, 50) - (150,150), NewColor,BF
    line (50, 50) - (150,150), rgb(0,0,0),B
    Flg = 0
    Draw String (170, 120), "R:   " & ((NewColor Shr 16) And 255), rgb(0,0,0)
    Draw String (170, 132), "G:   " & ((NewColor Shr 8) And 255), rgb(0,0,0)
    Draw String (170, 144), "B:   " & (NewColor And 255), rgb(0,0,0)
end if
IF KeyPress(FB.SC_F1) then
    H = H + 5
    IF H > 360 then H = 360
    Flg = 1
end if
IF KeyPress(FB.SC_F2) then
    H = H - 5
    IF H < 0 then H = 0
    Flg = 1
end if
IF KeyPress(FB.SC_F3) then
    S = S + 1
    IF S > 100 then S = 100
    Flg = 1
end if
IF KeyPress(FB.SC_F4) then
    S = S - 1
    IF S < 0 then S = 0
    Flg = 1
end if
IF KeyPress(FB.SC_F5) then
    VL = VL + 1
    IF VL > 100 then VL = 100
    Flg = 1
end if
IF KeyPress(FB.SC_F6) then
    VL = VL - 1
    IF VL < 0 then VL = 0
    Flg = 1
end if
IF KeyPress(FB.SC_F10) then
    IF mdl then
        mdl = 0
    Else
        mdl = 1
    end if
    Flg = 1
end if
loop until KeyPress(FB.SC_ESCAPE)
sleep
Conexion
Posts: 236
Joined: Feb 23, 2006 6:04

Post by Conexion »

Post Reply