我想逐渐给矩形着色,例如底部边缘为红色,左边缘为蓝色。
\fill[left color=blue,bottom color=red] (0,0) rectangle (2,2);
因此颜色应该随方位角变化,就像色轮以左下角为中心一样。可以这样做吗?
谢谢。
答案1
我不喜欢 Postscript,它让我头疼。无论如何,这是另一个线性插值着色。您也可以在 TikZ 图片中使用它。
\documentclass[]{article}
\usepackage{tikz}
\pgfdeclarefunctionalshading[vcol,hcol]{azimuth}{\pgfpointorigin}%
{\pgfpoint{4cm}{4cm}}{\pgfshadecolortorgb{vcol}{\mycola}\pgfshadecolortorgb{hcol}{\mycolb}}{
atan 90 div dup dup
\mycolablue \mycolbblue sub exch mul \mycolablue exch sub 3 1 roll
\mycolagreen \mycolbgreen sub mul \mycolagreen exch sub exch
\mycolared \mycolbred sub mul \mycolared exch sub 3 1 roll exch
}
\begin{document}
\colorlet{vcol}{black}
\colorlet{hcol}{yellow}
\pgfuseshading{azimuth}
%
\colorlet{vcol}{red}
\colorlet{hcol}{green}
\pgfuseshading{azimuth} % Ow, colorblind highness
%
\colorlet{vcol}{white}
\colorlet{hcol}{blue}
\pgfuseshading{azimuth}
\end{document}
在 TikZ 图片中使用此阴影的示例;
\documentclass[tikz]{standalone}
\tikzset{vcol/.code={\colorlet{vcol}{#1}},hcol/.code={\colorlet{hcol}{#1}},vcol=red,hcol=blue}
\pgfdeclarefunctionalshading[vcol,hcol]{azimuth}{\pgfpointorigin}%
{\pgfpoint{50bp}{50bp}}%
{%
\pgfshadecolortorgb{vcol}{\mycola}%
\pgfshadecolortorgb{hcol}{\mycolb}
}{%
atan 90 div dup dup
\mycolablue \mycolbblue sub mul \mycolablue exch sub 3 1 roll
\mycolagreen \mycolbgreen sub mul \mycolagreen exch sub exch
\mycolared \mycolbred sub mul \mycolared exch sub 3 1 roll exch
}
\begin{document}
\begin{tikzpicture}[shading=azimuth]
% Start modern art
\shade[vcol=yellow,hcol=black] (0,0) rectangle (5,3);
\shade[vcol=red!50!yellow,hcol=blue!80!purple!70!black] (-3,0) circle (1.5);
\shade[vcol=orange,hcol=green!50!blue] (-3,0) ++ (150:3.5) -- ++(150:1cm)
arc (150:90:4.5cm) coordinate(a)
--++(-40:3cm)|-++(0,-2cm)
arc (20:110:3cm)--cycle;
\end{tikzpicture}
\end{document}
请注意,在复杂的路径中,会发生一些严重的剪切、缩放和平移。因此,这最适合正方形。此外,这很可能会使您的打印机不满意,因为其中许多打印机的 PS 解释器不兼容。请先测试这些页面。是的,我是色盲,所以不要介意这些例子。
答案2
一种可能的方法:通过绘制大量纯色的细三角形来伪造渐变,这些三角形的颜色会从红色平滑地变为蓝色。生成的“圆盘”可以剪裁为矩形,如下例所示:
\begin{tikzpicture}
\path[clip] (0,0) rectangle (1,0.7);
\foreach \i in {0,0.5,...,90} {
\pgfmathsetmacro{\shade}{100/(90-0)*\i}
\fill[draw=none,blue!\shade!red] (0:0) -- (\i:1.4) -- (\i+1:1.4) -- cycle;
}
\draw (0,0) rectangle (1,0.7);
\end{tikzpicture}
答案3
另一种方法是使用 PGFPlots 绘制曲面图:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
hide axis,
axis equal image,
xmax=pi, ymax=pi,
samples y=6,
colormap={redblue}{color(0cm)=(red); color(1cm)=(blue)}
]
\addplot3 [
surf, shader=interp,
domain=0:90,
y domain=0:2*pi,
data cs=polar
] (x,y,x);
\end{axis}
\end{tikzpicture}
\end{document}
这样就可以指定其他颜色渐变。例如,您可以获得从红色开始,在 30 度时变为黄色,然后变为蓝色的渐变:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
hide axis,
axis equal image,
xmax=pi, ymax=pi,
samples y=6, samples=60,
colormap={redyellowblue}{color(0cm)=(red); color(0.33333cm)=(yellow); color(1cm)=(blue)}
]
\addplot3 [
surf, shader=interp,
domain=0:90,
y domain=0:2*pi,
data cs=polar
] (x,y,x);
\end{axis}
\end{tikzpicture}
\end{document}