diff options
author | wm4 <wm4@nowhere> | 2012-11-17 19:58:57 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2012-11-20 18:00:15 +0100 |
commit | d23b620a89e88b0ec0d6a66c7f05c2e461579647 (patch) | |
tree | 1d0719823527f5789aab4ae26cfa4ddcf72d9096 /core/m_option.c | |
parent | dcd2435e795eefe687c23b5b74da430525ad8c00 (diff) | |
download | mpv-d23b620a89e88b0ec0d6a66c7f05c2e461579647.tar.bz2 mpv-d23b620a89e88b0ec0d6a66c7f05c2e461579647.tar.xz |
m_option: add color option type
This accepts HTML-style hex colors in the form #RRGGBB. It's also
possible to provide an alpha component with #AARRGGBB. Each 2-digit
group is a hex number, which gives the color value from 0-255 (e.g.
There is existing code in subassconvert.c, which parses HTML-style
color values in SRT subs. This is not used: it's probably better if
option parsing is completely separate from code specific to certain
subtitle formats, even if a little code is duplicated.
Diffstat (limited to 'core/m_option.c')
-rw-r--r-- | core/m_option.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/core/m_option.c b/core/m_option.c index 58e93dfff9..90d8bb0359 100644 --- a/core/m_option.c +++ b/core/m_option.c @@ -1116,6 +1116,53 @@ const m_option_type_t m_option_type_subconfig_struct = { .parse = parse_subconf, }; +static int parse_color(const m_option_t *opt, struct bstr name, + struct bstr param, void *dst) +{ + if (param.len == 0) + return M_OPT_MISSING_PARAM; + + bstr val = param; + struct m_color color = {0}; + + if (bstr_eatstart0(&val, "#")) { + // #[AA]RRGGBB + if (val.len != 6 && val.len != 8) + goto error; + bool has_alpha = val.len == 8; + uint32_t c = bstrtoll(val, &val, 16); + if (val.len) + goto error; + color = (struct m_color) { + (c >> 16) & 0xFF, + (c >> 8) & 0xFF, + c & 0xFF, + has_alpha ? (c >> 24) & 0xFF : 0xFF, + }; + } else { + goto error; + } + + if (dst) + *((struct m_color *)dst) = color; + + return 1; + +error: + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Option %.*s: invalid color: '%.*s'\n", + BSTR_P(name), BSTR_P(param)); + mp_msg(MSGT_CFGPARSER, MSGL_ERR, + "Valid colors must be in the form #RRRGGBB or #AARRGGBB (in hex)\n"); + return M_OPT_INVALID; +} + +const m_option_type_t m_option_type_color = { + .name = "Color", + .size = sizeof(struct m_color), + .parse = parse_color, +}; + #include "video/img_format.h" static int parse_imgfmt(const m_option_t *opt, struct bstr name, |