1

I am trying to detect color of different elements in a webpage(saved on machine). Currently I am trying to write a code in python. The initial approach which I followed is:

  1. find color word in html file in different tags using regular expressions.
  2. try to read the hex value.

But this approach is very stupid. I am new to website design, can you please help me with this.

  • I don't favor that approach since "color" is ambiguous. Perhaps instead of looking at the static page, you should investigate the DOM tree and see what the background color is being computed as. – Makoto Nov 25 '12 at 04:28
  • Hey can you please put more light on DOM structure. I just read about it and it seems to be a good option. Can you please provide a good tutorial about it having examples. – user1802036 Nov 25 '12 at 04:48
  • How do you decide from which elements you want to get the color? Are you doing research or something? – eminor Nov 25 '12 at 05:09
  • I want colors of element like background, text on a website to transform them to make them more distinguishable for color blind people. – user1802036 Nov 25 '12 at 05:11
  • So you want to write a browser addon? – eminor Nov 25 '12 at 05:28
  • nopes..I want the page to be processed at an intermediate machine not at the user. – user1802036 Nov 25 '12 at 05:36
  • You could possibly do it by providing a service and receive the URI from the user, get the page source from the server, parse it, analyse the content, deliver the page to the user in a frame, let the user select an element, receive the id or class of the selected element (or add one), change the color and deliver the page again. But I think an addon is the better solution, because it's simpler to program, works with https connections and privacy can be guaranteed. – eminor Nov 25 '12 at 06:52

3 Answers3

1

There can be multiple stylesheets, and many cascading styles. You don't know which elements visually end up being the "background" elements. I think if you're looking for something robust that will work on most webpages, you need to leverage a browsers rendering engine and focus on identifying what a user would see.

Consider using a web browser to render the page, taking a screen shot, and then doing image processing to find the most frequent color near the sides of the page. You can use a scriptable browser like phantomjs.

If you're new to programming, this approach is going to be wayyyyy over your head.

goat
  • 31,486
  • 7
  • 73
  • 96
  • If the DOM is over their head, image processing likely is, too. – Makoto Nov 25 '12 at 04:38
  • I was going through some of the given answers, here is what I found the closest one. http://stackoverflow.com/questions/1887104/how-to-get-the-background-color-of-an-element-using-javascript – user1802036 Nov 25 '12 at 04:39
  • Image processing would be a really complex approach for this I guess. – user1802036 Nov 25 '12 at 04:41
  • ya, it's complicated. The problem with using just the javascript dom is that you don't know which element ends up being the element who's background is visible. This is a difficult task, and even with image processing, it's still difficult to identify the background color because some webpages don't have one clear color. – goat Nov 25 '12 at 04:58
  • But can we find some basic information like background color and text color on web page using java? – user1802036 Nov 25 '12 at 05:06
0

In java you can use JSOUP. Its quite good

Document doc = Jsoup.connect("http://YourPage.html").get();
Elements colors = doc.select("[bgcolor]"); 
Shamis Shukoor
  • 2,515
  • 5
  • 29
  • 33
-1

I don't know anything about Java or Python, but could you have it parse the html code and look for something like 'background-color: < color >'?

Stephen S
  • 166
  • 1
  • 1
  • 12
  • Thats what I am trying to do so far but many number of possibilities are making the code heavy and difficult to find out. – user1802036 Nov 25 '12 at 04:28