xparsecolor.c (2364B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 21#include "terminal/named-colors.h" 22#include "terminal/palette.h" 23 24#include <stdio.h> 25 26int guac_terminal_xparsecolor(const char* spec, 27 guac_terminal_color* color) { 28 29 int red; 30 int green; 31 int blue; 32 33 /* 12-bit RGB ("rgb:h/h/h"), zero-padded to 24-bit */ 34 if (sscanf(spec, "rgb:%1x/%1x/%1x", &red, &green, &blue) == 3) { 35 color->palette_index = -1; /* Not from palette. */ 36 color->red = red << 4; 37 color->green = green << 4; 38 color->blue = blue << 4; 39 return 0; 40 } 41 42 /* 24-bit RGB ("rgb:hh/hh/hh") */ 43 if (sscanf(spec, "rgb:%2x/%2x/%2x", &red, &green, &blue) == 3) { 44 color->palette_index = -1; /* Not from palette. */ 45 color->red = red; 46 color->green = green; 47 color->blue = blue; 48 return 0; 49 } 50 51 /* 36-bit RGB ("rgb:hhh/hhh/hhh"), truncated to 24-bit */ 52 if (sscanf(spec, "rgb:%3x/%3x/%3x", &red, &green, &blue) == 3) { 53 color->palette_index = -1; /* Not from palette. */ 54 color->red = red >> 4; 55 color->green = green >> 4; 56 color->blue = blue >> 4; 57 return 0; 58 } 59 60 /* 48-bit RGB ("rgb:hhhh/hhhh/hhhh"), truncated to 24-bit */ 61 if (sscanf(spec, "rgb:%4x/%4x/%4x", &red, &green, &blue) == 3) { 62 color->palette_index = -1; /* Not from palette. */ 63 color->red = red >> 8; 64 color->green = green >> 8; 65 color->blue = blue >> 8; 66 return 0; 67 } 68 69 /* If not RGB, search for color by name */ 70 return guac_terminal_find_color(spec, color); 71 72} 73