Store RGB colors in MySQL. Better like char or int?

I use PHP to query CSS settings from a MySQL database, which is then reflected in the CSS stylesheet. Example snippet below:

<?php
    $connect = //connect string
    $query = ($connect, "SELECT bgcolor, font FROM displays WHERE name = 'mySettings'");
    while ($row = mysqli_query($query)){
        $bgcolor = $row[bgcolor];
        $font = $row[font];
    }
    echo '
        body {
            background-color: #'.$bgcolor.';
            font: '.$font.';
        }
    ';
?>

The “displays” table has one column for each CSS property set. Each line represents a set of color / font / padding / margin settings saved by the user.

The font column is the varchar (50) data type. What data type should be "bgcolor"? CSS wants to see a hexadecimal value, but MySQL does not have a hexadecimal data type. A logical choice would be to save the value as an int type and use the HEX () function in the SELECT statement.

, , / , char (6)? PHP , . , ; CSS.

, int char?

+4
3

:

, CSS , , , , :

#ab382d

"#" CSS, ,

  • transparent
  • blue
  • rgb(100, 100, 100)
  • ( background-color)

, .

EDIT: , , , , .

+12

24- 32- RGB 32- , int, - CSS- .

Psuedocode:

 UInt32 rgba  = color.R | ( color.G << 8 ) | ( color.B << 16 ) | ( color.A << 24 );
 Color  color = Color.FromRgba( rgba & 0xFF, ( rgba >> 8 ) & 0xFF, ( rgba >> 16 ) & 0xFF, ( rgba >> 24 ) & 0xFF );
+5

, :

, . TINYINT. tinyint 0..255 ( ), , (, , ..).

0
source

Source: https://habr.com/ru/post/1538776/


All Articles