• Colorless color in html. HTML tutorial. RGB colors. Safe palette colors

    Vlad Merzhevich

    In HTML, color is specified in one of two ways: using hexadecimal code and by the name of certain colors. The method based on the hexadecimal number system is predominantly used, as it is the most universal.

    Hexadecimal colors

    HTML uses hexadecimal numbers to specify colors. The hexadecimal system, unlike the decimal system, is based, as its name suggests, on the number 16. The numbers will be as follows: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C , D, E, F. Numbers from 10 to 15 are replaced by Latin letters. In table 6.1 shows the correspondence between decimal and hexadecimal numbers.

    Numbers greater than 15 in the hexadecimal system are formed by combining two numbers into one (Table 6.2). For example, the number 255 in decimal corresponds to the number FF in hexadecimal.

    To avoid confusion in defining the number system, a hexadecimal number is preceded by a hash symbol #, for example #aa69cc. In this case, the case does not matter, so it is permissible to write #F0F0F0 or #f0f0f0.

    A typical color used in HTML looks like this.

    Here the background color of the web page is set to #FA8E47. The hash symbol # in front of a number means it is hexadecimal. The first two digits (FA) define the red component of the color, the third through fourth digits (8E) define the green component, and the last two digits (47) define the blue component. The end result will be this color.

    F.A. + 8E + 47 = FA8E47

    Each of the three colors - red, green and blue - can take values ​​from 00 to FF, resulting in a total of 256 shades. Thus, the total number of colors can be 256x256x256 = 16,777,216 combinations. A color model based on red, green and blue components is called RGB (red, green, blue). This model is additive (from add - add), in which the addition of all three components forms the color white.

    To make it easier to navigate hexadecimal colors, take into account some rules.

    • If the values ​​of the color components are the same (for example: #D6D6D6), then the result will be a gray tint. The higher the number, the lighter the color, with values ​​ranging from #000000 (black) to #FFFFFF (white).
    • A bright red color is formed if the red component is made maximum (FF) and the remaining components are set to zero. A color with a value of #FF0000 is the reddest possible red shade. The same is true for green (#00FF00) and blue (#0000FF).
    • Yellow (#FFFF00) is made by mixing red and green. This is clearly visible on the color wheel (Fig. 6.1), which presents the primary colors (red, green, blue) and complementary or additional ones. These include yellow, cyan and violet (also called magenta). In general, any color can be obtained by mixing colors close to it. Thus, cyan (#00FFFF) is obtained by combining blue and green.

    Rice. 6.1. Color wheel

    Colors based on hexadecimal values ​​do not have to be empirically selected. For this purpose, a graphic editor that can work with different color models, for example, Adobe Photoshop, is suitable. In Fig. Figure 6.2 shows the window for selecting a color in this program; the resulting hexadecimal value of the current color is outlined with a line. You can copy and paste it into your code.

    Rice. 6.2. Window for choosing colors in Photoshop

    Web colors

    If you set the monitor's color rendering quality to 8-bit (256 colors), then the same color can be displayed differently in different browsers. This is due to the way graphics are displayed, when the browser works with its own palette and cannot show a color that is not in its palette. In this case, the color is replaced by a combination of pixels of other, close to it, colors that imitate the given one. To ensure that the color remains the same across different browsers, a palette of so-called web colors was introduced. Web colors are those colors for which each component - red, green and blue - is set to one of six values ​​- 0 (00), 51 (33), 102 (66), 153 (99), 204 (CC), 255 (FF). The hexadecimal value of this component is indicated in brackets. The total number of colors from all possible combinations gives 6x6x6 - 216 colors. An example web color is #33FF66.

    The main feature of web color is that it appears the same in all browsers. At the moment, the relevance of web colors is very small due to the improvement in the quality of monitors and the expansion of their capabilities.

    Colors by name

    To avoid having to remember a set of numbers, you can use the names of commonly used colors instead. In table 6.3 shows the names of popular color names.

    Table 6.3. Names of some colors
    Color name Color Description Hexadecimal value
    black Black #000000
    blue Blue #0000FF
    fuchsia Light purple #FF00FF
    gray Dark gray #808080
    green Green #008000
    lime Light green #00FF00
    maroon Dark red #800000
    navy Dark blue #000080
    olive Olive #808000
    purple Dark purple #800080
    red Red #FF0000
    silver Light gray #C0C0C0
    teal Blue-green #008080
    white White #FFFFFF
    yellow Yellow #FFFF00

    It doesn't matter whether you specify a color by its name or by using hexadecimal numbers. These methods are equal in their effect. Example 6.1 shows how to set the background and text colors of a web page.

    Example 6.1. Background and text color

    Colors

    Example text

    In this example, the background color is set using the bgcolor attribute of the tag , and the text color through the text attribute. For variety, the text attribute is set to a hexadecimal number, and the bgcolor attribute is set to the reserved keyword teal .

    Look carefully at the drawing. The background of the drop-down window is made translucent. This is a fairly common design technique. Let's think about how this can be implemented.

    Task

    Make the color cross-browser translucent.

    Solution

    The first thought in this situation is to use a png24 image with an already set translucency for the background. But this picture is completely unnecessary. You can do just fine without it (and therefore without an extra request to the server). Let's still try to find the optimal solution.

    The second thought is to use . But in this case it is not very convenient. After all, then not only the background, but also the inscriptions will become translucent. Yes, actually, the entire window at once.

    Of course, you can try to add an additional container and apply opacity only to it, but this HTML element will be intended only for decoration and will obviously be redundant. Is it possible to do without it?

    Of course you can! If you use RGBA.

    RGBA color description format

    CSS3 allows you to specify color using RGB and RGBA functions. In this case, we must indicate the proportion of each color component for which one byte is allocated (from 0 to 255, in case anyone doesn’t know).

    The syntax for this case is very simple:

    Background: rgb(0, 255, 0); /* pure green */

    For RGBA, a fourth parameter is added - alpha transparency (from 0 to 1).

    Background: rgba(255, 0, 0, 0.5); /* pure red with 50% transparency */

    Here it is, the solution to our problem. Just set the background color using rgba and everything will look the way we want. Without unnecessary pictures and elements!

    Where can I get these numbers?

    You can look at the components of color using Photoshop's eyedropper tool.


    About cross-browser compatibility

    Since the RGB function is much older than RGBA and has been present since the days of the CSS2 standard, to protect against the most ancient browsers, you can use the following duplicate construction:

    SomeBlock ( background: rgb(255, 0, 0); background: rgba(255, 0, 0, 0.5); )

    With this approach, the great-grandfathers of modern browsers will not have translucency, but the color itself will remain correct.

    You will have to take care of IE separately. Donkeys don't understand RGBA right up to version 8 inclusive.

    As always: land for the peasants, factories for the workers, and donkeys a crutch! In the form of .

    Of course, in combat conditions we put this rule into a separate CSS, which we connect.

    SomeBlock ( background:transparent; filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#80ff0000,endColorstr=#80ff0000); zoom: 1; )

    The trick is to specify the starting and ending colors as the same (ff0000 - red) and take advantage of the fact that you can set the alpha channel for the gradient in this filter (in the example, the value is 80).

    For reference: the filter uses the hexadecimal system and the code for a completely opaque color is FF (in decimal this is 255). Accordingly, hexadecimal 80 is decimal 128, i.e. 50% transparency.

    Tested in:

    • IE 6-9
    • Firefox 3+
    • Opera 10+
    • Safari 4
    • Chrome

    HEX/HTML

    HEX color is nothing but a hexadecimal representation of RGB.

    Colors are represented as three groups of hexadecimal digits, where each group is responsible for its own color: #112233, where 11 is red, 22 is green, 33 is blue. All values ​​must be between 00 and FF.

    Many applications allow a shortened form of hexadecimal color notation. If each of the three groups contains the same characters, for example #112233, then they can be written as #123.

    1. h1 ( color: #ff0000; ) /* red */
    2. h2 ( color: #00ff00; ) /* green */
    3. h3 ( color: #0000ff; ) /* blue */
    4. h4 ( color: #00f; ) /* same blue, shorthand */

    RGB

    The RGB (Red, Green, Blue) color space consists of all possible colors that can be created by mixing red, green, and blue. This model is popular in photography, television, and computer graphics.

    RGB values ​​are specified as an integer from 0 to 255. For example, rgb(0,0,255) is displayed as blue because the blue parameter is set to its highest value (255) and the others are set to 0.

    Some applications (particularly web browsers) support percentage recording of RGB values ​​(from 0% to 100%).

    1. h1 ( color: rgb(255, 0, 0); ) /* red */
    2. h2 ( color: rgb(0, 255, 0); ) /* green */
    3. h3 ( color: rgb(0, 0, 255); ) /* blue */
    4. h4 ( color: rgb(0%, 0%, 100%); ) /* same blue, percentage entry */

    RGB color values ​​are supported in all major browsers.

    RGBA

    Recently, modern browsers have learned to work with the RGBA color model - an extension of RGB with support for an alpha channel, which determines the opacity of an object.

    The RGBA color value is specified as: rgba(red, green, blue, alpha). The alpha parameter is a number ranging from 0.0 (fully transparent) to 1.0 (fully opaque).

    1. h1 ( color: rgb(0, 0, 255); ) /* blue in regular RGB */
    2. h2 ( color: rgba(0, 0, 255, 1); ) /* the same blue in RGBA, because opacity: 100% */
    3. h3 ( color: rgba(0, 0, 255, 0.5); ) /* opacity: 50% */
    4. h4 ( color: rgba(0, 0, 255, .155); ) /* opacity: 15.5% */
    5. h5 ( color: rgba(0, 0, 255, 0); ) /* completely transparent */

    RGBA is supported in IE9+, Firefox 3+, Chrome, Safari, and Opera 10+.

    HSL

    The HSL color model is a representation of the RGB model in a cylindrical coordinate system. HSL represents colors in a more intuitive and human-readable way than typical RGB. The model is often used in graphics applications, color palettes, and image analysis.

    HSL stands for Hue (color/hue), Saturation (saturation), Lightness/Luminance (lightness/lightness/luminosity, not to be confused with brightness).

    Hue specifies the position of the color on the color wheel (from 0 to 360). Saturation is the percentage value of the saturation (from 0% to 100%). Lightness is a percentage of lightness (from 0% to 100%).

    1. h1 ( color: hsl(120, 100%, 50%); ) /* green */
    2. h2 ( color: hsl(120, 100%, 75%); ) /* light green */
    3. h3 ( color: hsl(120, 100%, 25%); ) /* dark green */
    4. h4 ( color: hsl(120, 60%, 70%); ) /* pastel green */

    HSL is supported in IE9+, Firefox, Chrome, Safari, and Opera 10+.

    HSLA

    Similar to RGB/RGBA, HSL has an HSLA mode with alpha channel support to indicate the opacity of an object.

    The HSLA color value is specified as: hsla(hue, saturation, lightness, alpha). The alpha parameter is a number ranging from 0.0 (fully transparent) to 1.0 (fully opaque).

    1. h1 ( color: hsl(120, 100%, 50%); ) /* green in normal HSL */
    2. h2 ( color: hsla(120, 100%, 50%, 1); ) /* the same green in HSLA, because opacity: 100% */
    3. h3 ( color: hsla(120, 100%, 50%, 0.5); ) /* opacity: 50% */
    4. h4 ( color: hsla(120, 100%, 50%, .155); ) /* opacity: 15.5% */
    5. h5 ( color: hsla(120, 100%, 50%, 0); ) /* completely transparent */

    CMYK

    The CMYK color model is often associated with color printing and printing. CMYK (unlike RGB) is a subtractive model, meaning that higher values ​​are associated with darker colors.

    Colors are determined by the ratio of cyan (Cyan), magenta (Magenta), yellow (Yellow), with the addition of black (Key/blacK).

    Each of the numbers that define a color in CMYK represents the percentage of ink of a given color that makes up the color combination, or more precisely, the size of the screen dot that is output on a phototypesetting machine on film of a given color (or directly on the printing plate in the case of CTP).

    For example, to obtain PANTONE 7526 color, you would mix 9 parts cyan, 83 parts magenta, 100 parts yellow, and 46 parts black. This can be denoted as follows: (9,83,100,46). Sometimes the following designations are used: C9M83Y100K46, or (9%, 83%, 100%, 46%), or (0.09/0.83/1.0/0.46).

    HSB/HSV

    HSB (also known as HSV) is similar to HSL, but they are two different color models. They are both based on cylindrical geometry, but HSB/HSV is based on the "hexcone" model, while HSL is based on the "bi-hexcone" model. Artists often prefer to use this model, it is generally accepted that the HSB/HSV device is closer to the natural perception of colors. In particular, the HSB color model is used in Adobe Photoshop.

    HSB/HSV stands for Hue (color/hue), Saturation (saturation), Brightness/Value (brightness/value).

    Hue specifies the position of the color on the color wheel (from 0 to 360). Saturation is the percentage value of the saturation (from 0% to 100%). Brightness is a percentage of brightness (from 0% to 100%).

    XYZ

    The XYZ color model (CIE 1931 XYZ) is a purely mathematical space. Unlike RGB, CMYK, and other models, in XYZ the principal components are “imaginary,” meaning you cannot associate X, Y, and Z with any set of colors to mix. XYZ is the master model for almost all other color models used in technical fields.

    LAB

    The LAB color model (CIELAB, “CIE 1976 L*a*b*”) is calculated from the CIE XYZ space. The design of Lab was designed to create a color space in which color changes would be more linear in terms of human perception (compared to XYZ), that is, so that the same change in color coordinate values ​​in different areas of the color space would produce the same sensation of color change.