Largest multiplier for color luminance weights.

  One time use code to find the largest possible multiplier without overflowing a DWORD in text color weighting worst case. The worst case being white (RGB og &h00FFFFFF). I have since figured out how I could have started closer to the final result, but this brute force method has served its purpose.

  One thing I have done is convert it from a console to a TXT.WINDOW so it can be compiled and run in PBWin or PBCC.


Source Code
#compile exe
#dim all
%UNICODE = 1
#if %def(%pb_cc32)
  #console off
#endif
'
function pbmain () as long
  local testWSum, MaxDword as quad
  'local MUT as extended
  local RIW, GIW, BIW as dword 'multiplied integer weights for each color
  local hTWin, MUT, MaxRedI, MaxGrnI, MaxBluI, TransPt as dword
  local ResultStr as string
  '
  txt.window ("Largest possible multiplied weights", 200, 150, 20, 68) to hTWin
  txt.print "any key to start"
  txt.waitkey$
  txt.cls
  MaxDword = int(2^32## - 1##) 'integer QUAD
  MUT =  round((2^32## - 1##) / (255## * 3##), 0) '
  txt.print format$(MUT); " Starting multiplier"
  '
  do
    RIW = round(MUT * 0.76245##, 0)
    GIW = round(MUT * 1.49685##, 0)
    BIW = round(MUT * 0.2907##, 0)
    MaxRedI = RIW * 255???
    MaxGrnI = GIW * 255???
    MaxBluI = BIW * 255???
    testWSum = MaxRedI + MaxGrnI + MaxBluI
    '
    if testWSum >= MaxDword then
      decr MUT
      RIW = round(MUT * 0.76245##, 0)
      GIW = round(MUT * 1.49685##, 0)
      BIW = round(MUT * 0.2907##, 0)
      MaxRedI = RIW * 255???
      MaxGrnI = GIW * 255???
      MaxBluI = BIW * 255???
      testWSum = MaxRedI + MaxGrnI + MaxBluI
      TransPt = round(MUT * 474.3##, 0)
      exit do
    else
      incr MUT
    end if
  loop
  '
  txt.print format$(MUT); " Multipier used."
  txt.print
  txt.print "Use in function Text_BlackOrWhite."
  txt.print "    Red multiplier: "; dec$(RIW)
  txt.print "  Green multiplier: "; dec$(GIW)
  txt.print "   Blue multiplier: "; dec$(BIW)
  txt.print "  Transition point: "; dec$(TransPt)
  txt.print
  txt.print "FYI:"
  txt.print "  "; dec$(testWSum); " sum of max colors"
  txt.print "  "; dec$(MaxDword); " dword max"
  txt.print "  "; format$(MaxDword / testWSum); " Just barely over 1 is good.
  txt.print
  txt.print "The numbers needed are in the clipboard as:"
  ResultStr = "  R-" + dec$(RIW) + " | G-" + dec$(GIW) + " | B-" + dec$(BIW) + _
              " | TP-" + dec$(TransPt) + " | mult-" + dec$(MUT)
  clipboard set text ResultStr
  txt.print ResultStr
  '
  txt.print
  txt.print "any key to exit"
  txt.waitkey$
  txt.end
end function
'

Full copyleft (ɔ), 2024 by Dale Yarker in source or compiled form. Complete license in new tab or window.


Created on 01 June 2024.

Done with this window?