Use Index for colour parsing.

This commit is contained in:
flash 2023-07-06 23:08:31 +00:00
parent d9094f0d05
commit ef9e439e19
8 changed files with 30 additions and 482 deletions

View file

@ -174,8 +174,17 @@ final class v1_0 implements \Uiharu\IApi {
if($result->hasMediaType()) if($result->hasMediaType())
$resp->content_type = MediaTypeExts::toV1($result->getMediaType()); $resp->content_type = MediaTypeExts::toV1($result->getMediaType());
if($result->hasColour()) if($result->hasColour()) {
$resp->color = Colour::toHexString($result->getColour()); $colour = $result->getColour();
if($colour->getAlpha() < 1.0)
$colour = new ColourRGB(
$colour->getRed(),
$colour->getGreen(),
$colour->getBlue()
);
$resp->color = (string)$colour;
}
if($result->hasTitle()) if($result->hasTitle())
$resp->title = $result->getTitle(); $resp->title = $result->getTitle();
if($result->hasSiteName()) if($result->hasSiteName())

View file

@ -1,469 +0,0 @@
<?php
namespace Uiharu;
final class Colour {
public static function toHexString(int $colour): string {
return '#' . str_pad(dechex($colour), 6, '0', STR_PAD_LEFT);
}
public static function convertFromCSS(string $input): int {
$input = strtolower(trim($input));
if(empty($input))
return -1;
switch($input) {
// CSS Level 1
case "black":
return self::BLACK;
case "silver":
return self::SILVER;
case "gray":
case "grey": // CSS Level 3
return self::GREY;
case "white":
return self::WHITE;
case "maroon":
return self::MAROON;
case "red":
return self::RED;
case "purple":
return self::PURPLE;
case "fuchsia":
case "magenta": // CSS Level 3
return self::MAGENTA;
case "green":
return self::GREEN;
case "lime":
return self::LIME;
case "olive":
return self::OLIVE;
case "yellow":
return self::YELLOW;
case "navy":
return self::NAVY;
case "blue":
return self::BLUE;
case "teal":
return self::TEAL;
case "aqua":
case "cyan": // CSS Level 3
return self::CYAN;
// CSS Level 2
case "orange":
return self::ORANGE;
// CSS Level 3
case "aliceblue":
return self::ALICEBLUE;
case "antiquewhite":
return self::ANTIQUEWHITE;
case "aquamarine":
return self::AQUAMARINE;
case "azure":
return self::AZURE;
case "beige":
return self::BEIGE;
case "bisque":
return self::BISQUE;
case "blanchedalmond":
return self::BLANCHEDALMOND;
case "blueviolet":
return self::BLUEVIOLET;
case "brown":
return self::BROWN;
case "burlywood":
return self::BURLYWOOD;
case "cadetblue":
return self::CADETBLUE;
case "chartreuse":
return self::CHARTREUSE;
case "chocolate":
return self::CHOCOLATE;
case "coral":
return self::CORAL;
case "cornflowerblue":
return self::CORNFLOWERBLUE;
case "cornsilk":
return self::CORNSILK;
case "crimson":
return self::CRIMSON;
case "darkblue":
return self::DARKBLUE;
case "darkcyan":
return self::DARKCYAN;
case "darkgoldenrod":
return self::DARKGOLDENROD;
case "darkgrey":
case "darkgray":
return self::DARKGREY;
case "darkgreen":
return self::DARKGREEN;
case "darkkhaki":
return self::DARKKHAKI;
case "darkmagenta":
return self::DARKMAGENTA;
case "darkolivegreen":
return self::DARKOLIVEGREEN;
case "darkorange":
return self::DARKORANGE;
case "darkorchid":
return self::DARKORCHID;
case "darkred":
return self::DARKRED;
case "darksalmon":
return self::DARKSALMON;
case "darkseagreen":
return self::DARKSEAGREEN;
case "darkslateblue":
return self::DARKSLATEBLUE;
case "darkslategrey":
case "darkslategray":
return self::DARKSLATEGREY;
case "darkturquoise":
return self::DARKTURQUOISE;
case "darkviolet":
return self::DARKVIOLET;
case "deeppink":
return self::DEEPPINK;
case "deepskyblue":
return self::DEEPSKYBLUE;
case "dimgray":
case "dimgrey":
return self::DIMGREY;
case "dodgerblue":
return DodgerBluself::DODGERBLUE;
case "firebrick":
return self::FIREBRICK;
case "floralwhite":
return self::FLORALWHITE;
case "forestgreen":
return self::FORESTGREEN;
case "gainsboro":
return self::GAINSBORO;
case "ghostwhite":
return self::GHOSTWHITE;
case "gold":
return self::GOLD;
case "goldenrod":
return self::GOLDENROD;
case "greenyellow":
return self::GREENYELLOW;
case "honeydew":
return self::HONEYDEW;
case "hotpink":
return self::HOTPINK;
case "indianred":
return self::INDIANRED;
case "indigo":
return self::INDIGO;
case "ivory":
return self::IVORY;
case "khaki":
return self::KHAKI;
case "lavender":
return self::LAVENDER;
case "lavenderblush":
return self::LAVENDERBLUSH;
case "lawngreen":
return self::LAWNGREEN;
case "lemonchiffon":
return self::LEMONCHIFFON;
case "lightblue":
return self::LIGHTBLUE;
case "lightcoral":
return self::LIGHTCORAL;
case "lightcyan":
return self::LIGHTCYAN;
case "lightgoldenrodyellow":
return self::LIGHTGOLDENRODYELLOW;
case "lightgray":
case "lightgrey":
return self::LIGHTGREY;
case "lightgreen":
return self::LIGHTGREEN;
case "lightpink":
return self::LIGHTPINK;
case "lightsalmon":
return self::LIGHTSALMON;
case "lightseagreen":
return self::LIGHTSEAGREEN;
case "lightskyblue":
return self::LIGHTSEAGREEN;
case "lightslategray":
case "lightslategrey":
return self::LIGHTSLATEGREY;
case "lightsteelblue":
return self::LIGHTSTEELBLUE;
case "lightyellow":
return self::LIGHTYELLOW;
case "limegreen":
return self::LIMEGREEN;
case "linen":
return self::LINEN;
case "mediumaquamarine":
return self::MEDIUMAQUAMARINE;
case "mediumblue":
return self::MEDIUMBLUE;
case "mediumorchid":
return self::MEDIUMORCHID;
case "mediumpurple":
return self::MEDIUMPURPLE;
case "mediumseagreen":
return self::MEDIUMSEAGREEN;
case "mediumslateblue":
return self::MEDIUMSLATEBLUE;
case "mediumspringgreen":
return self::MEDIUMSPRINGGREEN;
case "mediumturquoise":
return self::MEDIUMTURQUOISE;
case "mediumvioletred":
return self::MEDIUMVIOLETRED;
case "midnightblue":
return self::MIDNIGHTBLUE;
case "mintcream":
return self::MINTCREAM;
case "mistyrose":
return self::MISTYROSE;
case "moccasin":
return self::MOCCASIN;
case "navajowhite":
return self::NAVAJOWHITE;
case "oldlace":
return self::OLDLACE;
case "olivedrab":
return self::OLIVEDRAB;
case "orangered":
return self::ORANGERED;
case "orchid":
return self::ORCHID;
case "palegoldenrod":
return self::PALEGOLDENROD;
case "palegreen":
return self::PALEGREEN;
case "paleturquoise":
return self::PALETURQUOISE;
case "palevioletred":
return self::PALEVIOLETRED;
case "papayawhip":
return self::PAPAYAWHIP;
case "peachpuff":
return self::PEACHPUFF;
case "peru":
return self::PERU;
case "pink":
return self::PINK;
case "plum":
return self::PLUM;
case "powderblue":
return self::POWDERBLUE;
case "rosybrown":
return self::ROSYBROWN;
case "royalblue":
return self::ROYALBLUE;
case "saddlebrown":
return self::SADDLEBROWN;
case "salmon":
return self::SALMON;
case "sandybrown":
return self::SANDYBROWN;
case "seagreen":
return self::SEAGREEN;
case "seashell":
return self::SEASHELL;
case "sienna":
return self::SIENNA;
case "skyblue":
return self::SKYBLUE;
case "slateblue":
return self::SLATEBLUE;
case "slategray":
case "slategrey":
return self::SLATEGREY;
case "snow":
return self::SNOW;
case "springgreen":
return self::SPRINGGREEN;
case "steelblue":
return self::STEELBLUE;
case "tan":
return self::TAN;
case "thistle":
return self::THISTLE;
case "tomato":
return self::TOMATO;
case "turquoise":
return self::TURQUOISE;
case "violet":
return self::VIOLET;
case "wheat":
return self::WHEAT;
case "whitesmoke":
return self::WHITESMOKE;
case "yellowgreen":
return self::YELLOWGREEN;
// CSS Level 4
case "rebeccapurple":
return self::REBECCAPURPLE;
}
// #xxxxxx
if(preg_match('/^#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})$/', $input, $matches)) {
return (hexdec($matches[1]) << 16)
| (hexdec($matches[2]) << 8)
| hexdec($matches[3]);
}
// #xxx
if(preg_match('/^#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])$/', $input, $matches)) {
return (hexdec($matches[1] . $matches[1]) << 16)
| (hexdec($matches[2] . $matches[2]) << 8)
| hexdec($matches[3] . $matches[3]);
}
// todo: bother with rgb and rgba someday
return 0;
}
public const BLACK = 0x000000;
public const SILVER = 0xC0C0C0;
public const GREY = 0x808080;
public const WHITE = 0xFFFFFF;
public const MAROON = 0x800000;
public const RED = 0xFF0000;
public const PURPLE = 0x800080;
public const MAGENTA = 0xFF00FF;
public const GREEN = 0x008000;
public const LIME = 0x00FF00;
public const OLIVE = 0x808000;
public const YELLOW = 0xFFFF00;
public const NAVY = 0x000080;
public const BLUE = 0x0000FF;
public const TEAL = 0x008080;
public const CYAN = 0x00FFFF;
public const ORANGE = 0xFFA500;
public const ALICEBLUE = 0xF0F8FF;
public const ANTIQUEWHITE = 0xFAEBD7;
public const AQUAMARINE = 0x7FFFD4;
public const AZURE = 0xF0FFFF;
public const BEIGE = 0xF5F5DC;
public const BISQUE = 0xFFE4C4;
public const BLANCHEDALMOND = 0xFFEBCD;
public const BLUEVIOLET = 0x8A2BE2;
public const BROWN = 0xA52A2A;
public const BURLYWOOD = 0xDEB887;
public const CADETBLUE = 0x5F9EA0;
public const CHARTREUSE = 0x7FFF00;
public const CHOCOLATE = 0xD2691E;
public const CORAL = 0xFF7F50;
public const CORNFLOWERBLUE = 0x6495ED;
public const CORNSILK = 0xFFF8DC;
public const CRIMSON = 0xDC143C;
public const DARKBLUE = 0x00008B;
public const DARKCYAN = 0x008B8B;
public const DARKGOLDENROD = 0xB8860B;
public const DARKGREY = 0xA9A9A9;
public const DARKGREEN = 0x006400;
public const DARKKHAKI = 0xBDB76B;
public const DARKMAGENTA = 0x8B008B;
public const DARKOLIVEGREEN = 0x556B2F;
public const DARKORANGE = 0xFF8C00;
public const DARKORCHID = 0x9932CC;
public const DARKRED = 0x8B0000;
public const DARKSALMON = 0xE9967A;
public const DARKSEAGREEN = 0x8FBC8F;
public const DARKSLATEBLUE = 0x483D8B;
public const DARKSLATEGREY = 0x2F4F4F;
public const DARKTURQUOISE = 0x00CED1;
public const DARKVIOLET = 0x9400D3;
public const DEEPPINK = 0xFF1493;
public const DEEPSKYBLUE = 0x00BFFF;
public const DIMGREY = 0x696969;
public const DODGERBLUE = 0x1E90FF;
public const FIREBRICK = 0xB22222;
public const FLORALWHITE = 0xFFFAF0;
public const FORESTGREEN = 0x228B22;
public const GAINSBORO = 0xDCDCDC;
public const GHOSTWHITE = 0xF8F8FF;
public const GOLD = 0xFFD700;
public const GOLDENROD = 0xDAA520;
public const GREENYELLOW = 0xADFF2F;
public const HONEYDEW = 0xF0FFF0;
public const HOTPINK = 0xFF69B4;
public const INDIANRED = 0xCD5C5C;
public const INDIGO = 0x4B0082;
public const IVORY = 0xFFFFF0;
public const KHAKI = 0xF0E68C;
public const LAVENDER = 0xE6E6FA;
public const LAVENDERBLUSH = 0xFFF0F5;
public const LAWNGREEN = 0x7CFC00;
public const LEMONCHIFFON = 0xFFFACD;
public const LIGHTBLUE = 0xADD8E6;
public const LIGHTCORAL = 0xF08080;
public const LIGHTCYAN = 0xE0FFFF;
public const LIGHTGOLDENRODYELLOW = 0xFAFAD2;
public const LIGHTGREY = 0xD3D3D3;
public const LIGHTGREEN = 0x90EE90;
public const LIGHTPINK = 0xFFB6C1;
public const LIGHTSALMON = 0xFFA07A;
public const LIGHTSEAGREEN = 0x20B2AA;
public const LIGHTSKYBLUE = 0x87CEFA;
public const LIGHTSLATEGREY = 0x778899;
public const LIGHTSTEELBLUE = 0xB0C4DE;
public const LIGHTYELLOW = 0xFFFFE0;
public const LIMEGREEN = 0x32CD32;
public const LINEN = 0xFAF0E6;
public const MEDIUMAQUAMARINE = 0x66CDAA;
public const MEDIUMBLUE = 0x0000CD;
public const MEDIUMORCHID = 0xBA55D3;
public const MEDIUMPURPLE = 0x9370DB;
public const MEDIUMSEAGREEN = 0x3CB371;
public const MEDIUMSLATEBLUE = 0x7B68EE;
public const MEDIUMSPRINGGREEN = 0x00FA9A;
public const MEDIUMTURQUOISE = 0x48D1CC;
public const MEDIUMVIOLETRED = 0xC71585;
public const MIDNIGHTBLUE = 0x191970;
public const MINTCREAM = 0xF5FFFA;
public const MISTYROSE = 0xFFE4E1;
public const MOCCASIN = 0xFFE4B5;
public const NAVAJOWHITE = 0xFFDEAD;
public const OLDLACE = 0xFDF5E6;
public const OLIVEDRAB = 0x6B8E23;
public const ORANGERED = 0xFF4500;
public const ORCHID = 0xDA70D6;
public const PALEGOLDENROD = 0xEEE8AA;
public const PALEGREEN = 0x98FB98;
public const PALETURQUOISE = 0xAFEEEE;
public const PALEVIOLETRED = 0xDB7093;
public const PAPAYAWHIP = 0xFFEFD5;
public const PEACHPUFF = 0xFFDAB9;
public const PERU = 0xCD853F;
public const PINK = 0xFFC0CD;
public const PLUM = 0xDDA0DD;
public const POWDERBLUE = 0xB0E0E6;
public const ROSYBROWN = 0xBC8F8F;
public const ROYALBLUE = 0x4169E1;
public const SADDLEBROWN = 0x8B4513;
public const SALMON = 0xFA8072;
public const SANDYBROWN = 0xF4A460;
public const SEAGREEN = 0x2E8B57;
public const SEASHELL = 0xFFF5EE;
public const SIENNA = 0xA0522D;
public const SKYBLUE = 0x87CEEB;
public const SLATEBLUE = 0x6A5ACD;
public const SLATEGREY = 0x708090;
public const SNOW = 0xFFFAFA;
public const SPRINGGREEN = 0x00FF7F;
public const STEELBLUE = 0x4682B4;
public const TAN = 0xD2B48C;
public const THISTLE = 0xD8BFD8;
public const TOMATO = 0xFF6347;
public const TURQUOISE = 0x40E0D0;
public const VIOLET = 0xEE82EE;
public const WHEAT = 0xF5DEB3;
public const WHITESMOKE = 0xF5F5F5;
public const YELLOWGREEN = 0x9ACD32;
public const REBECCAPURPLE = 0x663399;
}

View file

@ -2,6 +2,7 @@
namespace Uiharu; namespace Uiharu;
use Index\MediaType; use Index\MediaType;
use Index\Colour\Colour;
interface ILookupResult { interface ILookupResult {
function getUrl(): Url; function getUrl(): Url;
@ -11,7 +12,7 @@ interface ILookupResult {
function getMediaType(): MediaType; function getMediaType(): MediaType;
function hasColour(): bool; function hasColour(): bool;
function getColour(): int; function getColour(): Colour;
function hasTitle(): bool; function hasTitle(): bool;
function getTitle(): string; function getTitle(): string;

View file

@ -4,6 +4,8 @@ namespace Uiharu\Lookup;
use Uiharu\AudioTags; use Uiharu\AudioTags;
use Uiharu\Url; use Uiharu\Url;
use Index\MediaType; use Index\MediaType;
use Index\Colour\Colour;
use Index\Colour\ColourRGB;
final class EEPROMLookupResult implements \Uiharu\ILookupResult, \Uiharu\IHasMediaInfo { final class EEPROMLookupResult implements \Uiharu\ILookupResult, \Uiharu\IHasMediaInfo {
private Url $url; private Url $url;
@ -53,8 +55,8 @@ final class EEPROMLookupResult implements \Uiharu\ILookupResult, \Uiharu\IHasMed
public function hasColour(): bool { public function hasColour(): bool {
return true; return true;
} }
public function getColour(): int { public function getColour(): Colour {
return 0x8559A5; return ColourRGB::fromRawRGB(0x8559A5);
} }
public function hasTitle(): bool { public function hasTitle(): bool {

View file

@ -4,6 +4,8 @@ namespace Uiharu\Lookup;
use DOMElement; use DOMElement;
use Index\MediaType; use Index\MediaType;
use Uiharu\Url; use Uiharu\Url;
use Index\Colour\Colour;
use Index\Colour\ColourRGB;
final class NicoNicoLookupResult implements \Uiharu\ILookupResult { final class NicoNicoLookupResult implements \Uiharu\ILookupResult {
private DOMElement|false|null $title = null; private DOMElement|false|null $title = null;
@ -51,8 +53,8 @@ final class NicoNicoLookupResult implements \Uiharu\ILookupResult {
public function hasColour(): bool { public function hasColour(): bool {
return true; return true;
} }
public function getColour(): int { public function getColour(): Colour {
return 0x252525; return ColourRGB::fromRawRGB(0x252525);
} }
public function hasTitle(): bool { public function hasTitle(): bool {

View file

@ -3,6 +3,7 @@ namespace Uiharu\Lookup;
use Uiharu\Url; use Uiharu\Url;
use Index\MediaType; use Index\MediaType;
use Index\Colour\Colour;
abstract class WebLookupResult implements \Uiharu\ILookupResult { abstract class WebLookupResult implements \Uiharu\ILookupResult {
protected Url $url; protected Url $url;
@ -27,7 +28,7 @@ abstract class WebLookupResult implements \Uiharu\ILookupResult {
} }
public abstract function hasColour(): bool; public abstract function hasColour(): bool;
public abstract function getColour(): int; public abstract function getColour(): Colour;
public abstract function hasTitle(): bool; public abstract function hasTitle(): bool;
public abstract function getTitle(): string; public abstract function getTitle(): string;

View file

@ -1,9 +1,9 @@
<?php <?php
namespace Uiharu\Lookup; namespace Uiharu\Lookup;
use Uiharu\Colour;
use Uiharu\Url; use Uiharu\Url;
use Index\MediaType; use Index\MediaType;
use Index\Colour\Colour;
class WebLookupSiteResult extends WebLookupResult { class WebLookupSiteResult extends WebLookupResult {
private object $siteInfo; private object $siteInfo;
@ -20,8 +20,8 @@ class WebLookupSiteResult extends WebLookupResult {
public function hasColour(): bool { public function hasColour(): bool {
return !empty($this->siteInfo->colour); return !empty($this->siteInfo->colour);
} }
public function getColour(): int { public function getColour(): Colour {
return Colour::convertFromCSS($this->siteInfo->colour); return Colour::parse($this->siteInfo->colour);
} }
public function hasTitle(): bool { public function hasTitle(): bool {

View file

@ -4,6 +4,8 @@ namespace Uiharu\Lookup;
use RuntimeException; use RuntimeException;
use Uiharu\Url; use Uiharu\Url;
use Index\MediaType; use Index\MediaType;
use Index\Colour\Colour;
use Index\Colour\ColourRGB;
final class YouTubeLookupResult implements \Uiharu\ILookupResult { final class YouTubeLookupResult implements \Uiharu\ILookupResult {
public function __construct( public function __construct(
@ -61,8 +63,8 @@ final class YouTubeLookupResult implements \Uiharu\ILookupResult {
public function hasColour(): bool { public function hasColour(): bool {
return true; return true;
} }
public function getColour(): int { public function getColour(): Colour {
return 0xFF0000; return ColourRGB::fromRawRGB(0xFF0000);
} }
public function hasTitle(): bool { public function hasTitle(): bool {