Author Topic: fill in the color of an area with different color borders  (Read 2458 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
fill in the color of an area with different color borders
« on: August 13, 2018, 02:04:10 pm »
No source, just quest!

Hello. When creating pictures using commands in QB64, I encountered a problem while I needed to paint a certain area where the sides were not bordered by the same color as the PAINT command requires. I'm working on my own alternative for PAINT. So far, I have this suggestion: I get boundaries of color borders, no matter what the color is, then I create a virtual screen where are only painted borders between colors. Follows the scan and copiyng the different pixels to the original screen (for this are borders draw with different color from copyed area). However, this seems to me to be too harsh. Does anyone have a better suggestion or did anyone try to solve it?

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: fill in the color of an area with different color borders
« Reply #1 on: August 13, 2018, 02:11:32 pm »
Before you spend hours to code what you suggested, just take a look into the DRF-Polygons folder of my Libraries collection (see signature). In file polygons.bm you'll find a FillPolygon function which just need to know the polygon's corners.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: fill in the color of an area with different color borders
« Reply #2 on: August 13, 2018, 02:25:18 pm »
Thank You RhoSigma, perfect idea, i try it!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: fill in the color of an area with different color borders
« Reply #3 on: August 13, 2018, 02:39:13 pm »
Hi Petr,

I am presently working on some general Pathfinder code. Already the first part of it could be used to "Paint" from a given point A whether bounded or not by "Obstacles" this "Paint"ing can even be shaded/colored according to degree of separation from point "A" (going around obstacles would add to degree of separation). It would be, of course very much slower than normal paint because it is not build in but conditions can be modified to say whether to "PAINT" or not and in what manner. It should be easy to modify for your purpose but also "harsh" way to do things, as you put it.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: fill in the color of an area with different color borders
« Reply #4 on: August 13, 2018, 02:42:03 pm »
Only limitation with the polygon routines is, they just use INTEGERs  as color parameter, which is sufficient for upto 256 colors. If you wanna use it in 32 bit mode you have to go in and change it to LONGs.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: fill in the color of an area with different color borders
« Reply #5 on: August 13, 2018, 03:52:35 pm »
Here is hastey mod for a Paint like program:
Code: QB64: [Select]
  1. _TITLE "PathFinder Paint mod"
  2. 'QB64 X 64 version 1.2 20180228/86  from git b301f92
  3. 'started 2018-08-11 when Colbalt asked about A* pathfinder
  4. ' He has now 2018-08-12 posted a QB64 version that is nice!
  5.  
  6. ' 2018-08-11 started PathFinder 1
  7. ' 2018-08-12 almost working but buggy backtract to point A after point B is found.
  8. ' 2018-08-13 PathFinder Paint mod
  9. ' Mod and test first part of buggy PathFinder 1 as a custom PAINT program.
  10. ' This will Paint until point B has been found,
  11. ' one could put point B outside the ww, wh range of screen
  12. ' which is where the paint would stop with this code.
  13.  
  14.  
  15. DEFINT A-Z
  16. CONST ww = 800
  17. CONST wh = 600
  18. CONST sq = 50
  19. CONST maxx = 16 'image width
  20. CONST maxy = 12 'image height
  21.  
  22. SCREEN _NEWIMAGE(ww, wh, 32)
  23. _SCREENMOVE (1280 - ww) / 2 + 30, (760 - wh) / 2
  24.  
  25. 'not using
  26. TYPE xytype
  27.     x AS INTEGER
  28.     y AS INTEGER
  29.  
  30. DIM SHARED ax, ay, bx, by 'start x, y and end x, y of path
  31.  
  32. 'board can be all or part of points from an image
  33. DIM SHARED board(1 TO maxx, 1 TO maxy) AS STRING * 6
  34.     'create a random board / image
  35.     FOR y = 1 TO maxy
  36.         FOR x = 1 TO maxx
  37.             board(x, y) = " "
  38.         NEXT
  39.     NEXT
  40.     'with these obstacles there is no guarantee a path will exist
  41.     'obstacles could be color values, dpending on how you DIM board
  42.     FOR i = 1 TO maxx * maxy * .7
  43.         ox = rand(1, maxx): oy = rand(1, maxy)
  44.         WHILE (ox = ax AND oy = ay) OR (ox = bx AND ox = by)
  45.             ox = rand(1, maxx): oy = rand(1, maxy)
  46.         WEND
  47.         board(ox, oy) = "O"
  48.     NEXT
  49.     ax = rand(1, maxx): ay = rand(1, maxy)
  50.     bx = rand(1, maxx): by = rand(1, maxy)
  51.     WHILE ax = bx AND ay = by
  52.         bx = rand(1, maxx): by = rand(1, maxy)
  53.     WEND
  54.     board(ax, ay) = "A"
  55.     board(bx, by) = "B"
  56.     displayB
  57.     _TITLE "Painting from Blue to Red until it is reached,  press esc to quit... spacebar to continue..."
  58.  
  59.     'display board and allow user to see blue start square and red stop square
  60.     WHILE NOT _KEYDOWN(32)
  61.         IF _KEYDOWN(27) THEN END
  62.         _LIMIT 100
  63.     WEND
  64.  
  65.     parentF = 1: tick = 0: parentx = 0
  66.     WHILE parentF = 1 AND parentx = 0
  67.         IF _KEYDOWN(27) THEN END
  68.         parentF = 0: tick = tick + 1
  69.         'IF tick > maxx * maxy THEN EXIT WHILE   'this was crude infinite loop stopper
  70.         ystart = max(ay - tick, 1): ystop = min(ay + tick, maxy)
  71.  
  72.         FOR y = ystart TO ystop
  73.             xstart = max(ax - tick, 1): xstop = min(ax + tick, maxx)
  74.             'PRINT ystart, ystop, xstart, xstop
  75.             'END
  76.             FOR x = xstart TO xstop
  77.                 'check out the neighbors
  78.                 IF x - 1 >= 1 THEN xxstart = x - 1 ELSE xxstart = x
  79.                 IF x + 1 <= maxx THEN xxstop = x + 1 ELSE xxstop = x
  80.                 IF y - 1 >= 1 THEN yystart = y - 1 ELSE yystart = y
  81.                 IF y + 1 <= maxy THEN yystop = y + 1 ELSE yystop = y
  82.                 IF RTRIM$(board(x, y)) = "" THEN 'unpainted spot, you can set any color rannge condition here
  83.                     changes$ = ""
  84.                     FOR yy = yystart TO yystop
  85.                         FOR xx = xxstart TO xxstop
  86.                             IF xx <> x OR yy <> y THEN
  87.  
  88.                                 'connection to a previous painted point or start A point?
  89.                                 IF RTRIM$(board(xx, yy)) = "A" OR INSTR(RTRIM$(board(xx, yy)), ",") > 0 THEN 'found a parent to assign to cell
  90.                                     'paint this point or rec
  91.                                     board(x, y) = LTRIM$(STR$(xx)) + "," + LTRIM$(STR$(yy))
  92.                                     frec (x - 1) * sq + 5, (y - 1) * sq + 5, x * sq - 5, y * sq - 5, _RGB32(60 + tick * 15, 60 + tick * 15, 60 + tick * 15)
  93.                                     parentF = 1 'so will continue looping
  94.                                 END IF
  95.  
  96.                             END IF
  97.                         NEXT
  98.                     NEXT
  99.  
  100.  
  101.                 ELSEIF RTRIM$(board(x, y)) = "A" OR INSTR(board(x, y), ",") > 0 THEN 'see if B is a neighbor
  102.  
  103.                     'do we have a condition to quit painting, otherwise will quit when screen is full or no new parent is assigned
  104.                     FOR yy = yystart TO yystop
  105.                         FOR xx = xxstart TO xxstop
  106.                             IF xx <> x OR yy <> y THEN
  107.                                 IF RTRIM$(board(xx, yy)) = "B" THEN 'B conects to x, y
  108.                                     parentx = x: parenty = y 'from this we should be able to backtrack to A
  109.                                     EXIT WHILE
  110.                                 END IF
  111.                             END IF
  112.                         NEXT
  113.                     NEXT
  114.                 END IF
  115.             NEXT
  116.         NEXT
  117.         '_DISPLAY
  118.         _LIMIT 2
  119.     WEND
  120.     IF parentx = 0 THEN
  121.         rgb 999
  122.         LOCATE 10, 15: PRINT "No connection from A to B, press esc to quit... spacebar to continue..."
  123.     END IF
  124.     WHILE NOT _KEYDOWN(32)
  125.         IF _KEYDOWN(27) THEN END
  126.         _LIMIT 100
  127.     WEND
  128.  
  129. SUB displayB
  130.     CLS
  131.     FOR y = 1 TO maxy
  132.         FOR x = 1 TO maxx
  133.             SELECT CASE RTRIM$(board(x, y))
  134.                 CASE IS = "": k& = _RGB32(0, 0, 0)
  135.                 CASE IS = "A": k& = _RGB32(0, 0, 200)
  136.                 CASE IS = "B": k& = _RGB32(230, 0, 0)
  137.                 CASE IS = "O": k& = _RGB32(250, 255, 0)
  138.                     'CASE ELSE: k = 30
  139.             END SELECT
  140.             frec (x - 1) * sq + 5, (y - 1) * sq + 5, x * sq - 5, y * sq - 5, k&
  141.         NEXT
  142.     NEXT
  143. SUB rec (x1, y1, x2, y2, rgbN&)
  144.     LINE (x1, y1)-(x2, y2), rgbN&, B
  145. SUB frec (x1, y1, w, h, rgbN&)
  146.     LINE (x1, y1)-(w, h), rgbN&, BF
  147. SUB rgb (n) ' New (even less typing!) New Color System 1000 colors with up to 3 digits
  148.     s3$ = RIGHT$("000" + LTRIM$(STR$(n)), 3)
  149.     r = VAL(MID$(s3$, 1, 1)): IF r THEN r = 28 * r + 3
  150.     g = VAL(MID$(s3$, 2, 1)): IF g THEN g = 28 * g + 3
  151.     b = VAL(MID$(s3$, 3, 1)): IF b THEN b = 28 * b + 3
  152.     COLOR _RGB32(r, g, b)
  153. FUNCTION rand% (lo%, hi%)
  154.     rand% = INT(RND * (hi% - lo% + 1)) + lo%
  155. FUNCTION min (n1, n2)
  156.     IF n1 > n2 THEN min = n2 ELSE min = n1
  157. FUNCTION max (n1, n2)
  158.     IF n1 < n2 THEN max = n2 ELSE max = n1
  159. FUNCTION leftOf$ (source$, of$)
  160.     posOf = INSTR(source$, of$)
  161.     IF posOf > 0 THEN leftOf$ = MID$(source$, 1, posOf - 1)
  162. FUNCTION rightOf$ (source$, of$)
  163.     posOf = INSTR(source$, of$)
  164.     IF posOf > 0 THEN rightOf$ = MID$(source$, posOf + LEN(of$))
  165.  
  166.  

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: fill in the color of an area with different color borders
« Reply #6 on: August 13, 2018, 04:28:19 pm »
Thank you all for your reply, i try it in own way, so here is VERY SLOW source code. Now i go to study RhoSigma and Bplus code.

Code: QB64: [Select]
  1. ' paint first pre - alternative. Program logic:
  2. '- User set coordinates and output color (border color is NOT NEED)
  3. '- Program find colors borders in easyest way. Create virtual screen and draw borders.
  4. '- Use QB64 statement PAINT in this virtual screen and then copy colored area back to visible screen
  5. '- Kill arrays and virtual screen and return back.
  6.  
  7. 'It is first version, extreme slow!
  8.  
  9. 'If is something "super", so has not big value. So this is named super.....slow.....wait........Sub :-D
  10. 'in true, i thing about - how do it many months. Because i have not enough time, i started this many times in different ways. First -   I wanted to do this by moving the X, Y pointer
  11. 'along the Y axis up to the border of the colors, and then copying the border of the color boundaries and reducing its radius at each turn. Unfortunately, when using a circle, the pointer
  12. 'has always run away. It was fun, but I did not to laugh ...
  13.  
  14. SCREEN _NEWIMAGE(800, 600, 256)
  15.  
  16.  
  17. CIRCLE (400, 300), 250, 78
  18. CIRCLE (400, 300), 450, 88
  19. LINE (100, 100)-(400, 400), 14, BF
  20. LINE (300, 300)-(500, 500), 25, B
  21.  
  22.  
  23. SuperPaint 338, 349, 12
  24. SuperPaint 531, 219, 25
  25. SuperPaint 436, 451, 11
  26. SuperPaint 715, 242, 110
  27.  
  28. PRINT "press any key"
  29.  
  30. LINE (100, 100)-(400, 400), 14, BF
  31. LINE (300, 300)-(500, 500), 25, B
  32. 'in position 350, 350 is now area with different color borders. Standard PAINT statement can not be used.
  33.  
  34. SuperPaint 350, 350, 60
  35.  
  36. SuperPaint 408, 1, 99
  37. SuperPaint 779, 15, 9
  38.  
  39.  
  40.  
  41.  
  42. SUB SuperPaint (x AS INTEGER, y AS INTEGER, clr AS LONG)
  43.     Background& = POINT(x, y)
  44.     W = _WIDTH(0)
  45.     H = _HEIGHT(0)
  46.     DIM O(W, H) AS INTEGER
  47.  
  48.     FOR Sy = 0 TO H - 1
  49.         FOR sx = 1 TO W - 2
  50.             IF POINT(sx, Sy) <> POINT(sx + 1, Sy) THEN
  51.                 oldsx = sx
  52.                 O(sx, Sy) = 1
  53.             END IF
  54.     NEXT sx, Sy
  55.  
  56.     FOR sx = 0 TO W - 1
  57.         FOR Sy = 0 TO H - 1
  58.             IF POINT(sx, Sy) <> POINT(sx, Sy + 1) THEN
  59.                 oldsy = Sy
  60.                 O(sx, Sy) = 1
  61.             END IF
  62.     NEXT Sy, sx
  63.     i = 0
  64.     bclr& = 255
  65.         CASE 1: bclr& = clr& - 1: Depth = 256
  66.         CASE 4: bclr& = _RGB32(clr& - 1, clr&, clr&): Depth = 32
  67.     END SELECT
  68.     my& = _DEST
  69.     virtual& = _NEWIMAGE(W, H, Depth)
  70.     _DEST virtual&
  71.     FOR Sy = 0 TO H
  72.         FOR sx = 0 TO W
  73.             IF O(sx, Sy) THEN
  74.                 PSET (sx, Sy), bclr&
  75.             END IF
  76.     NEXT sx, Sy
  77.     _DEST virtual&
  78.     PAINT (x, y), clr&, bclr&
  79.     ERASE O
  80.     FOR sx = 0 TO W
  81.         FOR Sy = 0 TO H
  82.             _SOURCE virtual&
  83.             _DEST my&
  84.             IF POINT(sx, Sy) = clr& THEN PSET (sx, Sy), clr&
  85.             IF POINT(sx - 1, Sy) = clr& THEN PSET (sx, Sy), clr&
  86.             IF POINT(sx, Sy - 1) = clr& THEN PSET (sx, Sy), clr&
  87.     NEXT Sy, sx
  88.     _DEST my&
  89.     _FREEIMAGE virtual&
  90.  

FellippeHeitor

  • Guest
Re: fill in the color of an area with different color borders
« Reply #7 on: August 13, 2018, 04:43:27 pm »