2

and parsing a HTML file. But i need to exclude grey color, i did it for black as if now but the grey color have alot of variation to give in if condition may i know how can i use it.

if style.find("color")>=0:
        aux = style[style.find("color"):]
        aux = aux[0:aux.find(";") + 1]
        
        if aux.find('#000000') < 0 and aux.find('black') < 0 and aux.find('#000') < 0: 
            use_raw = '%s%s' % (use_raw, aux)

This how i am exlcuding black, but here i want to exclude grey too.

Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169

2 Answers2

2

You can use a regex. ^#([a-fA-F0-9]{1,2})\1\1$ will identify all 6-digit and 3-digit gray hex colors.


Explanation

Identifying 'grey'

All grey hex color codes have 3 sets of 2 digits or 3 identical digits. e.g. #151515, #1C1C1C, #2E2E2E, #424242, #555, #EEE..... and this is how we can identify them as grey.

Determining if string meets 'grey' format

Therefore I would use a regex. The following regular expression will match all strings, that start with a #, and contain 3 identical consecutive sets of 2 digit alpha-numeric characters. Hence match all greys.

^#([a-fA-F0-9]{1,2})\1\1$

Applying with Python

import re
pattern = re.compile("^#([a-fA-F0-9]{1,2})\1\1$")
pattern.match(aux)

See the Python documentation on regular expressions: https://docs.python.org/3/library/re.html

References:

Community
  • 1
  • 1
Alicia Sykes
  • 5,997
  • 7
  • 36
  • 64
0

You could possibly do something like

    for i in ["lightgray", "lightgrey", "silver", "darkgray", "darkgrey",
            "gray", "grey", "dimgray", "dimgrey", "darkslategrey"]:
        if aux.find(i)<0:
            use_raw = '%s%s' % (use_raw, aux)
    for i in range(1,256):
        if aux.find('#%02x%02x%02x'%(i,i,i))<0 and format(i<<16 | i<<8 | i, '06X')<0 and format(i<<16 | i<<8 | i, '06x')<0:
            use_raw = '%s%s' % (use_raw, aux)

You could probably make the code smaller and more efficient, but that's a simple answer, I guess. (This does include only strictly grey colour. It does not allow for slight variations.)

Withaika
  • 31
  • 4
  • @RachitAgarwal, the list is to cover all bases. The for function with the range is designed to cover all hexa 3-digit, 6-digit, and RGB colour codes. – Withaika Apr 26 '17 at 21:39