Consulting

Results 1 to 7 of 7

Thread: Solved: excels AND function

  1. #1
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    291
    Location

    Solved: excels AND function

    I was reading an excel vba book and saw a function which turned a color value ie 16777216 into the RGB format 256 256 256. It used expressions such as :-

    ColorValue \ 256 ^ 0 And 255

    I thought the And function only returns true or false. In this case how could it perform the transformation required instead of just returning a series of booleans

    Any ideas?

    Cheers
    Phil

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, AND is a BIT operator in VB when acting upon numeric values.

    1 AND 1 returns 1, any other combination returns 0.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    291
    Location
    thanks xld,

    but how does it end up returning 256?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It can't, the most it can return is 255, which it would if ColorValue is 255.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    291
    Location
    Hi,

    Thanks for that. Ive seen it return 256 256 256 for the colour black.

    If ColorValue is 255 the calculation ColorValue\255 would return 0

    0 ^ 0 would return 1 (in fact anything ^ 0 returns 1)

    1 And 255 would return 0

    Is my logic incorrect. How would it return anything but 0 unless ColorValue is 255 in which case it would return 255.

    Thus it seems the only return values are 0 and 255 but I have seen it return all values between 0 and 256???

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It seems to me to be the same as

    ColorValue AND 255

    which will return whatever is in ColorValue, but I am only seeing a code fragment
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    I think that

    1 AND 255 would return 0
    would return 1 for bitwise AND operator

    1 = 00000001
    255 = 11111111
    AND = 00000001

    The most an 8 bit byte can hold is 255 decimal

    If you're seeing 256, you're probably looking at a long (32 bits) or an interger (16 bits), and ADDing (not ANDing) so that 255 + 1 will = 256

    Color Hex values are stored as longs

    This is a little function to seperate the long color values into their RGB components, and while the 3 array elements are typed as Long, they never exceed 255

    [VBA]
    Function Color2RGB(c As Long) As Variant
    Dim RGB(1 To 3) As Long

    RGB(1) = c Mod C256
    RGB(2) = (c \ C256) Mod 256
    RGB(3) = (c \ C256 \ C256) Mod 256

    Color2RGB = RGB
    End Function
    [/VBA]

    But as our Distinguished Lord of VBAX says, more of the code, including the variable (DIM) declarations, would be helpful

    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •