__PACKAGE__->register_color_space(
'xterm'
);
my
@color
;
sub
_init_colors
{
my
@colnames
;
@colnames
= (
qw(
x11:black x11:red3 x11:green3 x11:yellow3
x11:blue2 x11:magenta3 x11:cyan3 x11:gray90
x11:gray50 x11:red x11:green x11:yellow
rgb8:5C5CFF x11:magenta x11:cyan x11:white
)
);
}
else
{
@colnames
= (
qw(
rgb8:000000 rgb8:cd0000 rgb8:00cd00 rgb8:cdcd00
rgb8:0000ee rgb8:cd00cd rgb8:00cdcd rgb8:e5e5e5
rgb8:7f7f7f rgb8:ff0000 rgb8:00ff00 rgb8:ffff00
rgb8:5c5cff rgb8:ff00ff rgb8:00ffff rgb8:ffffff
)
);
}
foreach
my
$index
( 0 ..
$#colnames
)
{
my
$c_tmp
= Convert::Color->new(
$colnames
[
$index
] );
$color
[
$index
] = __PACKAGE__->SUPER::new(
$c_tmp
->as_rgb8->rgb8 );
$color
[
$index
]->[3] =
$index
;
}
foreach
my
$red
( 0 .. 5 ) {
foreach
my
$green
( 0 .. 5 ) {
foreach
my
$blue
( 0 .. 5 ) {
my
$index
= 16 + (
$red
*36) + (
$green
*6) +
$blue
;
$color
[
$index
] = __PACKAGE__->SUPER::new(
map
{
$_
?
$_
*40 + 55 : 0 } (
$red
,
$green
,
$blue
)
);
$color
[
$index
]->[3] =
$index
;
}
}
}
foreach
my
$grey
( 0 .. 23 ) {
my
$index
= 232 +
$grey
;
my
$whiteness
=
$grey
*10 + 8;
$color
[
$index
] = __PACKAGE__->SUPER::new(
$whiteness
,
$whiteness
,
$whiteness
);
$color
[
$index
]->[3] =
$index
;
}
}
__PACKAGE__->register_palette(
enumerate_once
=>
sub
{
@color
or _init_colors;
@color
},
);
sub
_index_or_percent
{
my
(
$name
,
$val
,
$max
) =
@_
;
if
(
$val
=~ m/^(\d+)%$/ ) {
$1 <= 100 or croak
"Convert::Color::XTerm: Invalid percentage for $name: '$val'"
;
return
int
(
$max
* $1 / 100 );
}
elsif
(
$val
=~ m/^(\d+)$/ ) {
$1 <=
$max
or croak
"Convert::Color::XTerm: Invalid index for $name: '$val'"
;
return
$1;
}
else
{
croak
"Convert::Color::XTerm: Invalid value for $name: '$val'"
;
}
}
sub
new
{
my
$class
=
shift
;
@_
== 1 or
croak
"usage: Convert::Color::XTerm->new( INDEX )"
;
@color
or _init_colors;
if
(
$_
[0] =~ m/^grey\((.*)\)$/ ) {
my
$grey
= _index_or_percent(
grey
=> $1, 23 );
return
$color
[232 +
$grey
];
}
elsif
(
$_
[0] =~ m/^rgb\((.*),(.*),(.*)\)$/ ) {
my
$red
= _index_or_percent(
red
=> $1, 5 );
my
$green
= _index_or_percent(
green
=> $2, 5 );
my
$blue
= _index_or_percent(
blue
=> $3, 5 );
return
$color
[16 + 36
*$red
+ 6
*$green
+
$blue
];
}
elsif
(
$_
[0] =~ m/^(\d+)$/ ) {
my
$index
= $1;
$index
>= 0 and
$index
< 256 or
croak
"No such XTerm color at index '$index'"
;
return
$color
[
$index
];
}
else
{
croak
"Convert::Color::XTerm: Expected index, grey() or rgb() specification, got '$_[0]'"
;
}
}
sub
index
{
my
$self
=
shift
;
return
$self
->[3];
}
0x55AA;