ClickHouse log coloring


Reading time: about 1 minute

ClickHouse has really nice colorful logs. They’re randomly generated to have a random color component, but consistent brightness. The code for that can be found here.

std::string setColor(UInt64 hash)
{
    /// Make a random RGB color that has constant brightness.
    /// https://en.wikipedia.org/wiki/YCbCr

    /// Note that this is darker than the middle relative luminance, see "Gamma_correction" and "Luma_(video)".
    /// It still looks awesome.
    UInt8 y = 128;

    UInt8 cb = static_cast<UInt8>(hash % 256);
    UInt8 cr = static_cast<UInt8>(hash / 256 % 256);

    UInt8 r = static_cast<UInt8>(std::max(0.0, std::min(255.0, y + 1.402 * (cr - 128))));
    UInt8 g = static_cast<UInt8>(std::max(0.0, std::min(255.0, y - 0.344136 * (cb - 128) - 0.714136 * (cr - 128))));
    UInt8 b = static_cast<UInt8>(std::max(0.0, std::min(255.0, y + 1.772 * (cb - 128))));

    /// ANSI escape sequence to set 24-bit foreground font color in terminal.
    return "\033[38;2;" + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b) + "m";
}

Citation

If you find this work useful, please cite it as:
@article{yaltirakli,
  title   = "ClickHouse log coloring",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2022",
  url     = "https://www.gkbrk.com/clickhouse-log-coloring"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "ClickHouse log coloring", March, 2022. [Online]. Available: https://www.gkbrk.com/clickhouse-log-coloring. [Accessed Dec. 27, 2024].
APA Style
Yaltirakli, G. (2022, March 15). ClickHouse log coloring. https://www.gkbrk.com/clickhouse-log-coloring
Bluebook Style
Gokberk Yaltirakli, ClickHouse log coloring, GKBRK.COM (Mar. 15, 2022), https://www.gkbrk.com/clickhouse-log-coloring

Comments

© 2024 Gokberk Yaltirakli