显示颜色及其名称

显示颜色及其名称

我想创建一个页面,其中 LaTeX 颜色与其名称对齐。有点类似于Overleaf 彩色手册页

在此处输入图片描述

具体来说:(1)我想为每个色场创建一个细的黑色边框,以便将与背景颜色对比度低的颜色分开——例如白色。

(2)然后,我希望使用打字机字体来显示颜色名称,而不影响表格的列名称,最好避免添加\texttt{...}到每个名称中。

(3)能够轻松改变彩色区域的大小。

它不需要以表格的形式构建。实用可读性是目标!我看到xcolor 手册通过创建列来实现这一点。但我无法理解这种机制,也无法重现这种情况。

我最好的成就是:

代码如下

\documentclass{scrartcl}

\usepackage[table,dvipsnames,x11names]{xcolor}  % colors with names
\usepackage{booktabs}

\begin{document}

\begin{table}
\centering
\texttt{
\begin{tabular}{ccc}
  \textbf{Color}                                   & \textbf{Name} & \textbf{xcolor option}\\
\midrule
  \textcolor{black}{\rule{.1\textwidth}{12pt}}     & black    &  \\
  \textcolor{darkgray}{\rule{.1\textwidth}{12pt}}  & darkgray &  \\
  \textcolor{gray}{\rule{.1\textwidth}{12pt}}      & gray     &  \\
  \textcolor{Cerulean}{\rule{.1\textwidth}{12pt}}  & Cerulean & divpsnames \\
  \textcolor{lightgray}{\rule{.1\textwidth}{12pt}} & lightgray&  \\
  \textcolor{Ivory1}{\rule{.1\textwidth}{12pt}}    & Ivory1   & x11names \\
  \textcolor{white}{\rule{.1\textwidth}{12pt}}     & white    &  \\
\end{tabular}
}
\caption{Some \LaTeX{} colors}
\end{table}

\end{document}

答案1

您可以使用collcell。我对表格做了其他调整。

\documentclass{article}

\usepackage[table,dvipsnames,x11names]{xcolor}  % colors with names
\usepackage{booktabs}
\usepackage{collcell}

% the group is in case you use \colorshow elsewhere
\newcommand{\colorshow}[1]{%
  \begingroup\setlength{\fboxsep}{0pt}%
  \fcolorbox{black}{#1}{\hspace{12pt}\vphantom{X}}%
  \endgroup
}

\begin{document}

\begin{table}
\centering

\begin{tabular}{
  @{}
  >{\collectcell\colorshow}c<{\endcollectcell}
  >{\ttfamily}l
  >{\ttfamily}l
  @{}
}
  \multicolumn{1}{@{}c}{\textbf{Color}} & 
  \multicolumn{1}{c}{\textbf{Name}} &
  \multicolumn{1}{c@{}}{\textbf{xcolor option}} \\
\midrule
  black     & black    &  \\
  darkgray  & darkgray &  \\
  gray      & gray     &  \\
  Cerulean  & Cerulean & divpsnames \\
  lightgray & lightgray&  \\
  Ivory1    & Ivory1   & x11names \\
  white     & white    &  \\
\end{tabular}

\caption{Some \LaTeX{} colors}

\end{table}

\end{document}

在此处输入图片描述

答案2

以备日后需要。

有些颜色没有名称。例如,在红绿蓝颜色模型中,三个通道中的每一个都可以取 256 个值,因此有 >16m 种组合。

另一方面,无论如何,人眼无法区分那么多的细节粒度,许多颜色值看起来都会相同。

即使每 56 种颜色都尝试一遍(比如说),它们也几乎无法区分(很大程度上也取决于渲染器和硬件)。

接近的颜色

灰度同样如此。

灰度

另外,谁有时间滚动浏览数千页的输出?因此,这些 1600 万种颜色和 256 种灰度只是锦上添花,并不是工具包的必需品。

然而,更实际的是,通过一列(预定义的)颜色名称,可以使用 expl3 sequence(=list)轻松显示相应的颜色,并使用映射函数对其进行迭代:

x11 颜色名称:

x11

平均能量损失

在 expl3 中作为一种“入门套件”完成,有意留有优化空间。颜色样本宽度可配置为命令选项,要跳过的颜色值步骤数也是如此。

打印颜色名称列表需要颜色名称列表,而 x11 预定义名称来自x11nam.def包文件xcolor

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{multicol}

\usepackage{xparse}%now in kernel

\ExplSyntaxOn

\int_new:N \l_doccol_red_int
\int_new:N \l_doccol_green_int
\int_new:N \l_doccol_blue_int
\int_new:N \l_doccol_gray_int
\fp_new:N \l_doccol_gray_fp

\int_new:N \l_doccol_step_int
\int_new:N \l_doccol_defaultstep_int
\int_set:Nn \l_doccol_defaultstep_int { 32 }

\tl_new:N \l_doccol_swatch_tl
\tl_new:N \l_doccol_defaultswatch_tl
\tl_set:Nn \l_doccol_defaultswatch_tl { 3em }




\NewDocumentCommand { \showrgb } { 
            O { \int_use:N \l_doccol_defaultstep_int } % size of step, e.g. 32
            O { \tl_use:N \l_doccol_defaultswatch_tl } % width of swatch, e.g. 3em
            } {

        \int_set:Nn \l_doccol_red_int { 0 }
        \int_set:Nn \l_doccol_green_int { 0 }
        \int_set:Nn \l_doccol_blue_int { 0 }
        \int_set:Nn \l_doccol_step_int { #1 }
        \tl_set:Nn \l_doccol_swatch_tl { #2 }
                
        \int_while_do:nn 
                { \l_doccol_red_int < 256 } 
                {%red
        \int_while_do:nn 
                { \l_doccol_green_int < 256 } 
                {%green
        \int_while_do:nn 
                { \l_doccol_blue_int < 256 } 
                {%blue
                        \definecolor{foo}{RGB}{
                            \int_use:N \l_doccol_red_int
                            ,\int_use:N \l_doccol_green_int
                            ,\int_use:N \l_doccol_blue_int
                        }
                            \center
                            \fcolorbox{blue}{foo}{ \makebox [ 
                                                \l_doccol_swatch_tl
                                                ]{
                                    \strut
                                    }}
                            \\
                            \group_begin:
                            \ttfamily
                            \small
                            r
                            \int_use:N \l_doccol_red_int
                            g
                            \int_use:N \l_doccol_green_int
                            b
                            \int_use:N \l_doccol_blue_int
                            \group_end:
                            \tex_par:D

                \int_add:Nn \l_doccol_blue_int { \l_doccol_step_int }

        }%end blue

                \int_set:Nn \l_doccol_blue_int { 0 }

                \int_add:Nn \l_doccol_green_int { \l_doccol_step_int }
                
        }%end green

        \int_set:Nn \l_doccol_green_int { 0 }
        
                \int_add:Nn \l_doccol_red_int { \l_doccol_step_int }


        }%end red
}


\NewDocumentCommand { \showgray } { 
            O { \int_use:N \l_doccol_defaultstep_int } 
            O { \tl_use:N \l_doccol_defaultswatch_tl }
            } {

        \int_set:Nn \l_doccol_gray_int { 0 }
        \int_set:Nn \l_doccol_step_int { #1 }
        \tl_set:Nn \l_doccol_swatch_tl { #2 }
                
        \int_while_do:nn 
                { \l_doccol_gray_int < 256 } 
                {%
                        \fp_set:Nn \l_doccol_gray_fp { \l_doccol_gray_int / 255 }
                        \definecolor{foo}{gray}{
                                    \fp_use:N \l_doccol_gray_fp
                        }
                            \center
                            \fcolorbox{blue}{foo}{ \makebox [ 
                                                \l_doccol_swatch_tl
                                                ]{
                                    \strut
                                    }}
                            \\
                            \group_begin:
                            \ttfamily
                            \small
                            g
                            \int_use:N \l_doccol_gray_int
                            \group_end:
                            \tex_par:D

    
                \int_add:Nn \l_doccol_gray_int { \l_doccol_step_int }

        }%end gray
}



%****************************************************
%*
%****************************************************
        \cs_generate_variant:Nn 
            \seq_gset_split:Nnn 
            { cno }



%****************************************************
%*
%****************************************************
%--------------------
\NewDocumentCommand { \mfsloadaseq } { o m +m } { 
% 1=namespace
% 2=seq name
% 3=data

                \IfNoValueTF { #1 } 
                        { \tl_clear:N \g_fc_namespace_tl } 
                        { \tl_gset:Nn \g_fc_namespace_tl { #1 } }


    \cs_if_free:cT
            { g_doccol \g_fc_namespace_tl #2 _seq }
            { \seq_new:c
                    { g_doccol \g_fc_namespace_tl #2 _seq } 
            }
    \seq_gclear:c 
            { g_doccol \g_fc_namespace_tl #2 _seq } 
    \seq_gset_split:cno 
            { g_doccol \g_fc_namespace_tl #2 _seq } 
            { , } 
            { #3 }

%   \seq_show:c 
%           { g_doccol \g_fc_namespace_tl #2 _seq } 

}


%****************************************************
%*
%****************************************************
%------------------
    \cs_set:Npn \dc_funccolbyname:n #1 { 
    % 1=colour name item from seq
                        \tl_set:Nx \l_tmpa_tl { #1 }
                            \center
                            \group_begin:
                            \fcolorbox{blue} { \tl_use:N \l_tmpa_tl } { \makebox [ 
                                                \l_doccol_swatch_tl
                                                ]{
                                    \strut
                                    }}
                            \\
                            \ttfamily
                            \small
                             \tl_use:N \l_tmpa_tl 
                            \group_end:
                            \tex_par:D

}






%****************************************************
%*
%****************************************************
%--------------------
\NewDocumentCommand { \showaseq }  { 
            O { \int_use:N \l_doccol_defaultstep_int } 
            O { \tl_use:N \l_doccol_defaultswatch_tl }
            O {}
            m
            } { 
% 1=step (not used)
% 2=width
% 3=namespace
% 4=seq name

        \int_set:Nn \l_doccol_step_int { #1 }
        \tl_set:Nn \l_doccol_swatch_tl { #2 }



                \IfNoValueTF { #3 } 
                        { \tl_clear:N \g_fc_namespace_tl } 
                        { \tl_gset:Nn \g_fc_namespace_tl { #3 } }

            \seq_map_function:cN 
                    { g_doccol \g_fc_namespace_tl #4 _seq } 
                    \dc_funccolbyname:n



}


\ExplSyntaxOff


\mfsloadaseq{x11}{
AntiqueWhite1
,AntiqueWhite2
,AntiqueWhite3
,AntiqueWhite4
,Aquamarine1
,Aquamarine2
,Aquamarine3
,Aquamarine4
,Azure1
,Azure2
,Azure3
,Azure4
,Bisque1
,Bisque2
,Bisque3
,Bisque4
,Blue1
,Blue2
,Blue3
,Blue4
,Brown1
,Brown2
,Brown3
,Brown4
,Burlywood1
,Burlywood2
,Burlywood3
,Burlywood4
,CadetBlue1
,CadetBlue2
,CadetBlue3
,CadetBlue4
,Chartreuse1
,Chartreuse2
,Chartreuse3
,Chartreuse4
,Chocolate1
,Chocolate2
,Chocolate3
,Chocolate4
,Coral1
,Coral2
,Coral3
,Coral4
,Cornsilk1
,Cornsilk2
,Cornsilk3
,Cornsilk4
,Cyan1
,Cyan2
,Cyan3
,Cyan4
,DarkGoldenrod1
,DarkGoldenrod2
,DarkGoldenrod3
,DarkGoldenrod4
,DarkOliveGreen1
,DarkOliveGreen2
,DarkOliveGreen3
,DarkOliveGreen4
,DarkOrange1
,DarkOrange2
,DarkOrange3
,DarkOrange4
,DarkOrchid1
,DarkOrchid2
,DarkOrchid3
,DarkOrchid4
,DarkSeaGreen1
,DarkSeaGreen2
,DarkSeaGreen3
,DarkSeaGreen4
,DarkSlateGray1
,DarkSlateGray2
,DarkSlateGray3
,DarkSlateGray4
,DeepPink1
,DeepPink2
,DeepPink3
,DeepPink4
,DeepSkyBlue1
,DeepSkyBlue2
,DeepSkyBlue3
,DeepSkyBlue4
,DodgerBlue1
,DodgerBlue2
,DodgerBlue3
,DodgerBlue4
,Firebrick1
,Firebrick2
,Firebrick3
,Firebrick4
,Gold1
,Gold2
,Gold3
,Gold4
,Goldenrod1
,Goldenrod2
,Goldenrod3
,Goldenrod4
,Green1
,Green2
,Green3
,Green4
,Honeydew1
,Honeydew2
,Honeydew3
,Honeydew4
,HotPink1
,HotPink2
,HotPink3
,HotPink4
,IndianRed1
,IndianRed2
,IndianRed3
,IndianRed4
,Ivory1
,Ivory2
,Ivory3
,Ivory4
,Khaki1
,Khaki2
,Khaki3
,Khaki4
,LavenderBlush1
,LavenderBlush2
,LavenderBlush3
,LavenderBlush4
,LemonChiffon1
,LemonChiffon2
,LemonChiffon3
,LemonChiffon4
,LightBlue1
,LightBlue2
,LightBlue3
,LightBlue4
,LightCyan1
,LightCyan2
,LightCyan3
,LightCyan4
,LightGoldenrod1
,LightGoldenrod2
,LightGoldenrod3
,LightGoldenrod4
,LightPink1
,LightPink2
,LightPink3
,LightPink4
,LightSalmon1
,LightSalmon2
,LightSalmon3
,LightSalmon4
,LightSkyBlue1
,LightSkyBlue2
,LightSkyBlue3
,LightSkyBlue4
,LightSteelBlue1
,LightSteelBlue2
,LightSteelBlue3
,LightSteelBlue4
,LightYellow1
,LightYellow2
,LightYellow3
,LightYellow4
,Magenta1
,Magenta2
,Magenta3
,Magenta4
,Maroon1
,Maroon2
,Maroon3
,Maroon4
,MediumOrchid1
,MediumOrchid2
,MediumOrchid3
,MediumOrchid4
,MediumPurple1
,MediumPurple2
,MediumPurple3
,MediumPurple4
,MistyRose1
,MistyRose2
,MistyRose3
,MistyRose4
,NavajoWhite1
,NavajoWhite2
,NavajoWhite3
,NavajoWhite4
,OliveDrab1
,OliveDrab2
,OliveDrab3
,OliveDrab4
,Orange1
,Orange2
,Orange3
,Orange4
,OrangeRed1
,OrangeRed2
,OrangeRed3
,OrangeRed4
,Orchid1
,Orchid2
,Orchid3
,Orchid4
,PaleGreen1
,PaleGreen2
,PaleGreen3
,PaleGreen4
,PaleTurquoise1
,PaleTurquoise2
,PaleTurquoise3
,PaleTurquoise4
,PaleVioletRed1
,PaleVioletRed2
,PaleVioletRed3
,PaleVioletRed4
,PeachPuff1
,PeachPuff2
,PeachPuff3
,PeachPuff4
,Pink1
,Pink2
,Pink3
,Pink4
,Plum1
,Plum2
,Plum3
,Plum4
,Purple1
,Purple2
,Purple3
,Purple4
,Red1
,Red2
,Red3
,Red4
,RosyBrown1
,RosyBrown2
,RosyBrown3
,RosyBrown4
,RoyalBlue1
,RoyalBlue2
,RoyalBlue3
,RoyalBlue4
,Salmon1
,Salmon2
,Salmon3
,Salmon4
,SeaGreen1
,SeaGreen2
,SeaGreen3
,SeaGreen4
,Seashell1
,Seashell2
,Seashell3
,Seashell4
,Sienna1
,Sienna2
,Sienna3
,Sienna4
,SkyBlue1
,SkyBlue2
,SkyBlue3
,SkyBlue4
,SlateBlue1
,SlateBlue2
,SlateBlue3
,SlateBlue4
,SlateGray1
,SlateGray2
,SlateGray3
,SlateGray4
,Snow1
,Snow2
,Snow3
,Snow4
,SpringGreen1
,SpringGreen2
,SpringGreen3
,SpringGreen4
,SteelBlue1
,SteelBlue2
,SteelBlue3
,SteelBlue4
,Tan1
,Tan2
,Tan3
,Tan4
,Thistle1
,Thistle2
,Thistle3
,Thistle4
,Tomato1
,Tomato2
,Tomato3
,Tomato4
,Turquoise1
,Turquoise2
,Turquoise3
,Turquoise4
,VioletRed1
,VioletRed2
,VioletRed3
,VioletRed4
,Wheat1
,Wheat2
,Wheat3
,Wheat4
,Yellow1
,Yellow2
,Yellow3
,Yellow4
,Gray0
,Green0
,Grey0
,Maroon0
,Purple0
}



\begin{document}
\section{RGB}
\begin{multicols}{5}
\showrgb[24][0.5in]
\end{multicols}

\newpage
\section{Grayscale}
\begin{multicols}{5}
\showgray[8][0.5in]
\end{multicols}


\newpage
\section{x11 Names}
\begin{multicols}{5}
\showaseq[0][0.5in]{x11}
\end{multicols}



\end{document}

相关内容