首页 > 解决方案 > Modelica 网页颜色

问题描述

我认为将一些颜色定义为稍后在 Modelica 颜色注释中使用的常量可能会很方便。看起来 web 颜色是一个公认的标准,我将它们全部复制到一个常量数组包中:

但似乎我无法按预期使用它们,以下示例失败。似乎文字工作,或者如果我在同一个类中定义常量,但如果我导入它则不行。我做错了什么,还是我正在使用的工具的限制?

within ;
package Modelica_Colors
  type RealColor = Modelica.Icons.TypeReal[3] (each min=0, each max=255);
  package BasicColors
    final constant RealColor Aqua={0,255,255};
    final constant RealColor Black={0,0,0};
    final constant RealColor Blue={0,0,255};
    final constant RealColor Fuchsia={255,0,255};
    final constant RealColor Gray={128,128,128};
    final constant RealColor Grey={128,128,128};
    final constant RealColor Green={0,128,0};
    final constant RealColor Lime={0,255,0};
    final constant RealColor Maroon={128,0,0};
    final constant RealColor Navy={0,0,128};
    final constant RealColor Olive={128,128,0};
    final constant RealColor Purple={128,0,128};
    final constant RealColor Red={255,0,0};
    final constant RealColor Silver={192,192,192};
    final constant RealColor Teal={0,128,128};
    final constant RealColor White={255,255,255};
    final constant RealColor Yellow={255,255,0};
  end BasicColors;

  package Colors
    final constant RealColor Crimson={220,20,60};
    final constant RealColor Cyan={0,255,255};
    final constant RealColor DarkBlue={0,0,139};
    final constant RealColor DarkCyan={0,139,139};
    final constant RealColor OliveDrab={107,142,35};
    final constant RealColor Orange={255,165,0};
    final constant RealColor OrangeRed={255,69,0};
    final constant RealColor Orchid={218,112,214};
    final constant RealColor PaleGoldenRod={238,232,170};
    final constant RealColor PaleGreen={152,251,152};
    final constant RealColor Yellow={255,255,0};
    final constant RealColor YellowGreen={154,205,50};
  end Colors;

model Test
  // works
  constant RealColor test_const1={255,20,147}; // pinkish

  // does not work
  final constant RealColor test_equal=Modelica_Colors.Colors.OrangeRed;
  constant RealColor test_pi={Modelica.Constants.pi,2*Modelica.Constants.pi,3*Modelica.Constants.pi};
  import Modelica_Colors.BasicColors.Yellow;

protected 
  // works
  constant RealColor test_const2={147,20,255};

  annotation (Icon(coordinateSystem(preserveAspectRatio=false), graphics={
          Rectangle(
          extent={{-100,100},{0,0}},
          lineColor=test_const2,
          fillColor=test_const1,
          fillPattern=FillPattern.Solid),
          
          Rectangle(
          extent={{0,0},{100,-100}},
          lineColor=Modelica_Colors.Colors.Cyan,
          fillColor=Yellow,
          fillPattern=FillPattern.Solid),
          
          Rectangle(
          extent={{0,100},{100,0}},
          lineColor=test_equal,
          fillColor=test_equal,
          fillPattern=FillPattern.Solid),
          
          Rectangle(
          extent={{-100,0},{0,-100}},
          lineColor=test_equal,
          fillColor=test_pi,
          fillPattern=FillPattern.Solid)}), Diagram(coordinateSystem(
          preserveAspectRatio=false)));

end Test;

  annotation (uses(Modelica(version="3.2.3")));
end Modelica_Colors;

标签: colorsannotationsmodelicadymola

解决方案


原因很简单:图形注释取决于变量,仅当变量存在于结果文件中时才有效。因此,由于您的颜色注释正在使用导入调用数据,因此黄色代码不包含在结果文件中,因此您的测试框无法着色。相同的逻辑可以应用于受保护的变量:它将无法应用于任何图形注释。


推荐阅读