Skip to content

Commit

Permalink
Renamed enum color to colors.
Browse files Browse the repository at this point in the history
Added enum colors conversion to rgb struct.
Added colors_test.cpp.
  • Loading branch information
Remotion committed Jun 8, 2018
1 parent 45fa4ee commit 0a04018
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
10 changes: 7 additions & 3 deletions include/fmt/colors.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

FMT_BEGIN_NAMESPACE

enum class colors : uint32_t;

// rgb is a struct for red, green and blue colors.
// We use rgb as name because some editors will show it as color direct in the
// editor.
Expand All @@ -25,6 +27,8 @@ struct rgb {
: r(r_), g(g_), b(b_) {}
FMT_CONSTEXPR_DECL rgb(uint32_t hex)
: r((hex >> 16) & 0xFF), g((hex >> 8) & 0xFF), b((hex) & 0xFF) {}
FMT_CONSTEXPR_DECL rgb(colors hex)
: r((uint32_t(hex) >> 16) & 0xFF), g((uint32_t(hex) >> 8) & 0xFF), b(uint32_t(hex) & 0xFF) {}
uint8_t r;
uint8_t g;
uint8_t b;
Expand All @@ -40,7 +44,7 @@ FMT_CONSTEXPR inline void to_esc(uint8_t c, char out[], int offset) {

} // namespace internal

FMT_FUNC void vprint_rgb(rgb fd, string_view format, format_args args) {
inline void vprint_rgb(rgb fd, string_view format, format_args args) {
char escape_fd[] = "\x1b[38;2;000;000;000m";
static FMT_CONSTEXPR_DECL const char RESET_COLOR[] = "\x1b[0m";
internal::to_esc(fd.r, escape_fd, 7);
Expand All @@ -52,7 +56,7 @@ FMT_FUNC void vprint_rgb(rgb fd, string_view format, format_args args) {
std::fputs(RESET_COLOR, stdout);
}

FMT_FUNC void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args) {
inline void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args) {
char escape_fd[] = "\x1b[38;2;000;000;000m"; // foreground color
char escape_bg[] = "\x1b[48;2;000;000;000m"; // background color
static FMT_CONSTEXPR_DECL const char RESET_COLOR[] = "\x1b[0m";
Expand Down Expand Up @@ -87,7 +91,7 @@ inline void print(rgb fd, rgb bg, string_view format_str, const Args & ... args)
vprint_rgb(fd, bg, format_str, make_format_args(args...));
}

enum class color : uint32_t {
enum class colors : uint32_t {
alice_blue = 0xF0F8FF, // rgb(240,248,255)
antique_white = 0xFAEBD7, // rgb(250,235,215)
aqua = 0x00FFFF, // rgb(0,255,255)
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ add_fmt_test(time-test)
add_fmt_test(util-test mock-allocator.h)
add_fmt_test(custom-formatter-test)
add_fmt_test(ranges-test)
add_fmt_test(colors_test)

if (HAVE_OPEN)
add_fmt_executable(posix-mock-test
Expand Down
38 changes: 38 additions & 0 deletions test/colors_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Formatting library for C++ - the core API
//
// Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.
//
// Copyright (c) 2018 - present, Remotion (Igor Schulz)
// All Rights Reserved
// {fmt} support for rgb color output.

#include "gtest.h"
#include "gtest-extra.h"

#include "fmt/colors.h"
#include "fmt/printf.h"

#include <vector>
#include <array>
#include <map>
#include <string>

TEST(ColorsTest, RgbTest) {
fmt::print(fmt::rgb(10,20,30), "rgb(10,20,30) \n"); // \x1b[38;2;010;020;030mrgb(10,20,30) \n\x1b[0m
fmt::print(fmt::rgb(255,20,30), "rgb(255,20,30) \n"); // \x1b[38;2;255;020;030mrgb(255,20,30) \n\x1b[0m
fmt::print(fmt::rgb(30,255,30), "rgb(30,255,30) \n"); // \x1b[38;2;030;255;030mrgb(30,255,30) \n\x1b[0m
fmt::print(fmt::rgb(30,30,255), "rgb(30,30,255) \n"); // \x1b[38;2;030;030;255mrgb(30,30,255) \n\x1b[0m

EXPECT_WRITE(stdout, fmt::print(fmt::rgb(255,20,30), "rgb(255,20,30)"), "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
}

TEST(ColorsTest, Colors) {
fmt::print(fmt::colors::blue,"blue \n"); // \x1b[38;2;000;000;255mblue \n\x1b[0m
fmt::print(fmt::colors::medium_spring_green,"medium_spring_green \n"); // \x1b[38;2;000;250;154mmedium_spring_green \n\x1b[0m
fmt::print(fmt::colors::light_golden_rod_yellow,"light_golden_rod_yellow \n"); // \x1b[38;2;250;250;210mlight_golden_rod_yellow \n\x1b[0m

EXPECT_WRITE(stdout, fmt::print(fmt::colors::blue,"blue"), "\x1b[38;2;000;000;255mblue\x1b[0m");
}

0 comments on commit 0a04018

Please sign in to comment.