Forum Discussion
Colin_McCrae: Do you mean this?
@HKosova .... yes! Thats the one. Thanks.
- MulgraveTester9 years agoFrequent Contributor
Thanks Colin and HKosova,
After posting I wrote this VB script to return the background colour as the most popular colour shown in an object.
I wanted to find the foreground colour by returning the 2nd most popular colour but due to anti-aliasing it is possible that none of the pixels match the foreground colour, even though it may not look that way to our eye. To resolve this I changed Windows to turn ClearType off and then I was presented with just two colours (foreground and background).
Hope this helps anyone else with the same issue.
function getColours(myObj)
'Identify the colours used in myObj. The most popular will be the background colour and the second most popular will be the foreground colour.
'NOTE: Turn off ClearType text in your windows settings otherwise anti-aliasing could mean that none of the colours are the true foreground colour
'Returns an array (Background Colour, Foreground Colour)
dim myPic
dim maxWidth, maxHeight
dim x,y,i, arrBounds
dim max1, max2 'Index of highest count and second highest count in colour popularity
redim colourStats(1,0) 'Colour, Count
colourStats(0,0) = 0
colourStats(1,0) = 0
set myPic = myObj.picture
maxWidth = myPic.size.width - 1
maxHeight = myPic.size.height - 1for x = 1 to maxWidth
for y = 1 to maxHeight
pixColour = myPic.pixels(x,y)
'Try to match this pixel to one already in the colourStats array
found = false
arrBounds = ubound(colourStats,2)
for i = 0 to arrBounds
if colourStats(0,i) = pixColour then 'If we have met this colour before
found = true
colourStats(1,i) = colourStats(1,i) + 1 'Increment the count for this colour
exit for
end if
next
if not found then 'Add the newly found colour to the colourStats array
redim preserve colourStats(1, arrBounds + 1)
colourStats(0, arrBounds + 1) = pixColour
colourStats(1, arrBounds + 1) = 1
end if
next
next'Search through the colourStats and return the most popular colours
arrBounds = ubound(colourStats,2)
max1 = 0
max2 = 0
for i = 0 to arrBounds
if colourStats(1,i) >= colourStats(1,max1) then
max2 = max1
max1 = i
end if
if colourStats(1,i) < colourStats(1,max1) and colourStats(1,i) >= colourStats(1,max2) then
max2 = i
end if
next
getColours = array(colourStats(0,max1), colourStats(0,max2)) 'Return an array showing background and foreground colour values
end functionsub test3
set myObj = <your object>
log.Picture myObj
objColours = getColours(myObj)
log.Checkpoint("Background colour is (" & paddedhex(objColours(0),6) & ").")
log.Checkpoint("Foreground colour is (" & paddedhex(objColours(1),6) & ").")
end subfunction paddedHex(intValue, charCount)
paddedHex = Right("00000000" & hex(intValue), charCount)
end function