matth
matth

Reputation: 2696

Modelica web colors

I thought it might be convenient to define some colors as constants to use later on in Modelica color annotations. As it seems the web colors are an accepted standard, I copied all of them into a package of constant arrays:

But it seems I cannot use them as intended, the following example fails. It seems literals work, or if I define the constant in the same class, but not if I import it. Am I doing something wrong, or is it a limitation of the tool I am using?

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;

Upvotes: 1

Views: 153

Answers (1)

Arnaud
Arnaud

Reputation: 29

the reason is simple: graphical annotations depending on variable, work only if the variable is present in the result file. So, as your color annotation is calling data using import, the Yellow code is not included inside the result file, hence your test box cannot be colored. The same logic can be applied to protected variables: it will fail to be applied to any graphical annotation.

Upvotes: 1

Related Questions