1<?php 2 3/** 4 * DokuWiki Barcodes Plugin 5 * Copyright (C) 2023 Matthias Lohr <mail@mlohr.com> 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21namespace DokuWiki\Barcodes; 22 23use ParseError; 24 25class Color { 26 27 private $red; 28 private $green; 29 private $blue; 30 31 public function __construct($color) 32 { 33 if (preg_match('/^#?([0-9a-f]{6})/i', $color, $matches)) { 34 $this->red = hexdec(substr($matches[1], 0, 2)); 35 $this->green = hexdec(substr($matches[1], 2, 2)); 36 $this->blue = hexdec(substr($matches[1], 4, 2)); 37 } 38 else { 39 throw new ParseError('Could not parse "' . $color . '" to a color'); 40 } 41 } 42 43 public static function parse($str) { 44 return new Color($str); 45 } 46 47 public static function str2hex($str) { 48 return (new Color($str))->getHex(); 49 } 50 51 public function getHex() { 52 return sprintf("%02X%02X%02X", $this->red, $this->green, $this->blue); 53 } 54 55 public function __toString() { 56 return $this->getHex(); 57 } 58} 59