home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / BlitzBasic / dark / lightning / test.mail / text0000.txt < prev   
Encoding:
Text File  |  1998-06-24  |  3.3 KB  |  118 lines

  1. Ok, this is the extendend version with dithering!
  2. Rate and r2 are the rates of proportionality  of
  3. dither colour usage. (How much % will be used for each colour)
  4. Anyway, this is *not* the correct rate. It is just an estimate.
  5. For a correct rate
  6. if the dithering pair is represented by the points A,B in space
  7. and point T represents the original color,
  8. then find point C where AB and TC intersect, with TC being
  9. perpendicular to AB, thus finding the ratio AC/BC
  10.  
  11. Altough that above gives you always the correct ratio,
  12. this one is almost as good. (but not always),
  13. but is a lot,lot faster.
  14. So, take a look:
  15.  
  16. *************************
  17.  
  18. SCREEN 11
  19. sources = 4
  20. targets = 8
  21. depth = 16
  22. RANDOMIZE TIMER
  23. DIM r(sources), g(sources), b(sources)
  24. DIM red(targets), green(targets), blue(targets)
  25. DIM rd(targets), gd(targets), bd(targets)
  26.  
  27. 'Setting up Initial colours
  28.  
  29. FOR n = 1 TO sources
  30.    r(n) = INT(RND * depth)
  31.    g(n) = INT(RND * depth)
  32.    b(n) = INT(RND * depth)
  33.    PRINT r(n), g(n), b(n)
  34. NEXT n
  35.  
  36. PRINT "-------------------------------------"
  37.  
  38. 'setting target colours
  39. FOR n = 1 TO targets
  40.    red(n) = INT(RND * depth)
  41.    green(n) = INT(RND * depth)
  42.    blue(n) = INT(RND * depth)
  43. PRINT red(n), green(n), blue(n)
  44. NEXT n
  45.  
  46. 'remap colours
  47. FOR m = 1 TO sources
  48.    PRINT "Source", r(m), g(m), b(m)
  49.    dd = -1
  50.    sel = -1
  51.       FOR n = 1 TO targets
  52.          dr = red(n) - r(m)
  53.          dg = green(n) - g(m)
  54.          db = blue(n) - b(m)
  55.          d = SQR(dr ^ 2 + dg ^ 2 + db ^ 2)
  56.          'PRINT red(n), green(n), blue(n), d
  57.          IF dd <> -1 THEN 'if there has been a previous selection
  58.             IF d < dd THEN 'compare selections
  59.                dd = d 'select new color if
  60.                sel = n 'new target closer to source color
  61.             END IF
  62.          ELSE        'if there was no previous selection
  63.             dd = d   'select new color
  64.             sel = n
  65.          END IF
  66.    NEXT n
  67. 'show selected color
  68.    PRINT "Target", red(sel), green(sel), blue(sel), dd
  69.  
  70. 'create table for possible dithers
  71.  
  72.    FOR n = 1 TO targets
  73.       rd(n) = (red(sel) + red(n)) / 2
  74.       gd(n) = (green(sel) + green(n)) / 2
  75.       bd(n) = (blue(sel) + blue(n)) / 2
  76.    NEXT n
  77.  
  78. 'find best dithering pair
  79.    dd2 = -1
  80.    sel2 = -1
  81.    FOR n = 1 TO targets
  82.          dr = rd(n) - r(m)
  83.          dg = gd(n) - g(m)
  84.          db = bd(n) - b(m)
  85.          d = SQR(dr ^ 2 + dg ^ 2 + db ^ 2)
  86. '         PRINT rd(n), gd(n), bd(n), d
  87.          IF dd2 <> -1 THEN 'if there has been a previous selection
  88.             IF d < dd2 THEN 'compare selections
  89.                dd2 = d 'select new color if
  90.                sel2 = n 'new target closer to source color
  91.             END IF
  92.          ELSE        'if there was no previous selection
  93.             dd2 = d   'select new color
  94.             sel2 = n
  95.          END IF
  96.    NEXT n
  97.    PRINT "Target2", red(sel2), green(sel2), blue(sel2)
  98.    PRINT "Average", rd(sel2), gd(sel2), bd(sel2), dd2
  99. 'Now, take a look at the distances
  100. 'to find the rate of proportionality
  101.    rate = dd2 / dd
  102.    PRINT "Rate: "; rate
  103. 'and that is the result of dithering
  104.    r2 = (1 - rate)
  105. 'the dithered colour
  106.    tr = red(sel) * rate + red(sel2) * r2
  107.    tg = green(sel) * rate + green(sel2) * r2
  108.    tb = blue(sel) * rate + blue(sel2) * r2
  109.    dr = tr - r(m)
  110.    dg = tg - g(m)
  111.    db = tb - b(m)
  112. 'the distance of the dithered colour
  113.    td = SQR(dr ^ 2 + dg ^ 2 + db ^ 2)
  114.    PRINT "Dithered", tr, tg, tb, td
  115. NEXT m
  116.  
  117.  
  118.