Liberty BASIC Community Forum
« [RC] (suggested tasks) Colour circle »

Welcome Guest. Please Login or Register.
Mar 26th, 2017, 1:58pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: [RC] (suggested tasks) Colour circle  (Read 113 times)
tenochtitlanuk
Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1137
xx [RC] (suggested tasks) Colour circle
« Thread started on: Feb 20th, 2017, 12:27pm »

Basically the task is to programmatically produce images like these.. which I can now easily do. BUT.. the following is a resume of several hours chasing silly solutions..
User Image

My first attempt was to plot a family of radial lines, cycling the RGB colour values up and down over 120 degree sectors. This leaves visible gaps, and increasing the line width leads to visible distortion at the centre.
User Image

I could have used piefilled of course, or drawn outline sectors and filled them, but I wanted to vary the fill radially, either up to white or down to black.

I managed to do this by modifying my Bresenham algorithm to fade the RGB components up/down depending on radius. But I still had the central region visible distorted.

The I realised- I should be scanning through the x,y space of my image, rather than through the r,theta space of the desired structure. Job done!

Code will go on my site.
User IP Logged

tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1609
xx Re: [RC] (suggested tasks) Colour circle
« Reply #1 on: Feb 21st, 2017, 03:19am »

John, I could not locate that task.
May be it got renamed and now named
Color wheel?

EDIT
the problem with task is it's vagueness.
"Write a function to draw a HSV color wheel" - what is agreed image that "HSV color wheel" should look like?
« Last Edit: Feb 21st, 2017, 03:36am by tsh73 » User IP Logged

The existence of bug reports means somebody is using the software and actually cares to report back to you that he is having a problem with it, instead of just deleting it from their hard disk.
(Janusz Marcin Gorycki)
tenochtitlanuk
Moderator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1137
xx Re: [RC] (suggested tasks) Colour circle
« Reply #2 on: Feb 21st, 2017, 04:31am »

Yeah, totally agree on the vagueness. Just reading the wikipedia stuff on colour spaces does my head in. They are basically three-dimensionable anyway.

It's only a 'suggested task' at present.
Quote:
Color wheel is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


I haven't uploaded most of my recent Rosetta Code 'solutions' to RC because I find it more interesting to follow the basic idea to wherever it takes me... I posted my programming journey here because it was a classic of heading off in the wrong direction, and only after various wrong turns approaching it the sensible way.

For me, the satisfaction here was to achieve interesting graphics, rather than restricting myself to a poorly defined task!

User Image

Following is the kind of code I've been using in all sorts of variations. The display of the colour number under the mouse is not used in this one.. code is only here, unpolished, to encourage others to play.
Code:
    '   **    colourWheelRethink2.bas   ***

    '   **    raster scan resulting image and calculate angle and radius to find appropriate colour RGB components

    nomainwin

    global hdc, col$, pi
    pi           =4 *atn( 1)
    ptSize       =1

    WindowWidth  =600
    WindowHeight =600

    graphicbox #w.gb, 10, 10, 540, 540

    open "Colour wheel" for window as #w

    h       =hwnd( #w.gb)
    calldll #user32, "GetDC", h as ulong, hdc as ulong

    #w "trapclose quit"

    #w.gb "down ; fill white ; size "; ptSize; " ; font Ubuntu_Mono 12 ; flush"
    #w.gb "when leftButtonDown [getPixel]"

    radius  =250

    for x =0 to 500 step ptSize
        for y =0 to 500 step ptSize
            angle   =atan2( y -250, x -250) *360 / 2 /pi        '   Angle in degrees round full circle....
            sector  =int( angle /60)                            '       ...split into six 60 degree sectors, labelled 0 to 5
            slope   =( angle mod 60) /60 *255                   '       ...and each split into 1 degree sectors.
            scan

            select case sector
                case 0
                    col$    ="255 ";                    str$( int( slope));      "   0"
                case 1
                    col$    =str$( int( 256 -slope)); " 255                          0"
                case 2
                    col$    ="0                         255 ";                     str$( int( slope))
                case 3
                    col$    ="0 ";                      str$( int( 256 -slope)); " 255"
                case 4
                    col$    =str$( int( slope));    "     0                        255"
                case 5
                    col$    ="255                         0 ";                     str$( int( 256 -slope))
            end select

            red     =val( word$( col$, 1)): grn =val( word$( col$, 2)): blu =val( word$( col$, 3))

            R       =( ( x -270)^2 +( y -270)^2)^0.5 /250

            redR    =R *int( red): grnR =R *int( grn): bluR =R *int( blu)     '   fade to rim black

            #w.gb "color "; str$( redR) +" " +str$( grnR) +" " +str$( bluR)
            if R >1 then  #w.gb "color white"
            #w.gb "set "; x; " "; y
        next y
    next x

    #w.gb "flush ; getbmp scr 0 0 540 540"
    bmpsave "scr", "colWheelToRimWhiteNoFade.bmp"

    wait

  [getPixel]
    xx      =MouseX
    yy      =MouseY
    calldll #gdi32, "GetPixel", hdc as ulong, xx as long, yy as long, pixcol as ulong
    #w.gb "up ; goto 20 20 ; down ; color black"
    #w.gb "\ " +right$( "          " +str$( pixcol), 10) +" "
    wait

    sub quit h$
        close #h$
        calldll #user32, "ReleaseDC", hw as ulong, hdc as ulong   'release the DC
        end
    end sub

function atan2( y, x)
    result$     ="Undetermined"
    If ( x =0) and ( y >0) then atan2 = pi /2:      result$ ="Determined"
    if ( x =0) and ( y <0) then atan2 =3 * pi /2:   result$ ="Determined"
    if ( x >0) and ( y =0) then atan2 =0:           result$ ="Determined"
    if ( x <0) and ( y =0) then atan2 =pi:          result$ ="Determined"

    If result$ <>"Determined" then
        if x =0 and y =0 then
            atan2 =0
        else
            baseAngle =atn( abs( y) /abs( x))
            If ( x >0) and ( y >0) then atan2 =       baseAngle
            If ( x <0) and ( y >0) then atan2 = pi   -baseAngle
            If ( x <0) and ( y <0) then atan2 = pi   +baseAngle
            If ( x >0) and ( y <0) then atan2 = 2*pi -baseAngle
        end if
    end if
end function

 

User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls