Think Python X2: Difference between revisions

From XPUB & Lens-Based wiki
Line 21: Line 21:
== Exercise X2.2 ==
== Exercise X2.2 ==


The most common way to represent colors in the computer is as a triplet of 3 numbers (or "components") where each number represents the level of red, green, and blue that when mixed together would produce the desired color. This triplet is called the RGB value of a color. Often the range of numbers is used between 0 (no color) and 255 (full color).<ref name="bits">A number between 0 and 255 takes exactly 8 bits of computer memory. An RGB value would then take 24 bits (8*3). Most graphics displays work with "24bit" color. In this system, more than a 16 million unique colors can be specified.</ref> For instance, pure red would be the triplet (255, 0, 0)<ref name="tuple">Python recognizes lists of things when written in parentheses ("("...")") and separated by commas (","). Tuples are dealt with later on in the textbook.</ref> and full-saturation yellow (red and green with no blue) would be (255, 255, 0).
The most common way to represent colors in the computer is as a triplet of 3 numbers (or "components") where each number represents the level of red, green, and blue that when mixed together would produce the desired color. This triplet is called the RGB value of a color. Often the range of numbers is used between 0 (no color) and 255 (full color).<ref name="bits">A number between 0 and 255 takes exactly 8 bits of computer memory. An RGB value would then take 24 bits (8*3). Most graphics displays work with "24bit" color. In this system, more than a 16 million unique colors can be specified.</ref> For instance, pure red would be the triplet (255, 0, 0)<ref name="tuple">Python recognizes lists of things when written in parentheses and separated by commas, as in(1, 2, 3). These are formally called "tuples" and are dealt with later on in the textbook.</ref> and full-saturation yellow (red and green with no blue) would be (255, 255, 0).


A simple way to "mix" two colors is to calculate the average of each color component.<ref name="colormixing">You can see the effect of this kind of transformation in an image editor by creating two layers with overlapping boxes in each color. If you then turn the opacity (or transparency) of one of the layers to 50%, you should see the resulting "mix" color.
A simple way to "mix" two colors is to calculate the average of each color component.<ref name="colormixing">You can see the effect of this kind of transformation in an image editor by creating two layers with overlapping boxes in each color. If you then turn the opacity (or transparency) of one of the layers to 50%, you should see the resulting "mix" color.

Revision as of 13:04, 6 October 2008

Exercise X2.1

A common problem when working with time-based media (audio / movies) is to translate between various representations of "timecodes". For instance, subtitles for movies can be written as a text file in the "SubRip" format, where the starting time of a particular subtitle looks like this:

01:12:08,000 --> 01:12:10,520
We both know that the mind
of a woman in love...

So, the format for a time code in a SubRip file is:

HH:MM:SS,FFF

Where the "HH" means the hour, "MM" the minutes, "SS" the seconds, and "FFF" the fractional part of the time (always written with three numbers).

Use python as a calculator to convert the following pair of time codes from an SRT file into a pair of number of seconds representing the same times (e.g. the timecode 00:01:30,000 ==> 90 seconds). Show the calculations you make and try to write each one as a single line (so one line of calculation per timecode).

Exercise X2.2

The most common way to represent colors in the computer is as a triplet of 3 numbers (or "components") where each number represents the level of red, green, and blue that when mixed together would produce the desired color. This triplet is called the RGB value of a color. Often the range of numbers is used between 0 (no color) and 255 (full color).[1] For instance, pure red would be the triplet (255, 0, 0)[2] and full-saturation yellow (red and green with no blue) would be (255, 255, 0).

A simple way to "mix" two colors is to calculate the average of each color component.[3] Calculate the color that would result from mixing yellow with pure blue and show your calculation as typed to Python. Based on the resulting numbers, can you already say anything about what the color might look like? You can use most image editing softwares like the GIMP or Photoshop to see the result.

Notes

  1. A number between 0 and 255 takes exactly 8 bits of computer memory. An RGB value would then take 24 bits (8*3). Most graphics displays work with "24bit" color. In this system, more than a 16 million unique colors can be specified.
  2. Python recognizes lists of things when written in parentheses and separated by commas, as in(1, 2, 3). These are formally called "tuples" and are dealt with later on in the textbook.
  3. You can see the effect of this kind of transformation in an image editor by creating two layers with overlapping boxes in each color. If you then turn the opacity (or transparency) of one of the layers to 50%, you should see the resulting "mix" color.

Answers

answers