🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Ocl hex to dec
This page was created by Hans.karlsen on 2021-02-09. Last edited by Wikiadmin on 2026-07-29.

You can use these OCL expressions to convert a hexadecimal HTML-style color value such as #e66465 into either one packed decimal number or three RGB decimal components.

Input format

Use a seven-character color string in the form #RRGGBB:

  • RR is the red component.
  • GG is the green component.
  • BB is the blue component.
  • Each component is a hexadecimal value from 00 through FF.

For example, #e66465 contains:

Component Hexadecimal digits Decimal value
Red e6 230
Green 64 100
Blue 65 101

The expressions convert each hexadecimal character by finding it in '#123456789abcdef'. The leading # occupies position zero, so the positions of 1 through f correspond to their hexadecimal values 1 through 15. The input is converted to lowercase first, which allows uppercase hexadecimal letters such as #E66465.

Convert hex to one packed decimal value

Use this expression when you need the complete RRGGBB value as one decimal number:

let parts='#e66465'.tolower.ToCharArray->collect(c|'#123456789abcdef'->tochararray->indexof0(c)) in(
  (parts.at0(1)*16+parts.at0(2))*256*256+
  (parts.at0(3)*16+parts.at0(4))*256+
  (parts.at0(5)*16+parts.at0(6))
)

For #e66465, the expression evaluates the color as:

230 * 256 * 256 + 100 * 256 + 101

The result is 15098981.

Use a value from your model

Replace the literal color string with the attribute or expression that holds your color value. For example, if the current object has a color expression in self.Color:

let parts=self.Color.tolower.ToCharArray->collect(c|'#123456789abcdef'->tochararray->indexof0(c)) in(
  (parts.at0(1)*16+parts.at0(2))*256*256+
  (parts.at0(3)*16+parts.at0(4))*256+
  (parts.at0(5)*16+parts.at0(6))
)

Convert hex to an RGB string

Use this expression when you need a comma-separated RGB value, for example 230,100,101:

let parts='#e66465'.tolower.ToCharArray->collect(c|'#123456789abcdef'->tochararray->indexof0(c)) in
(
(parts.at0(1)*16+parts.at0(2)).ToString()+','+
(parts.at0(3)*16+parts.at0(4)).ToString()+','+
(parts.at0(5)*16+parts.at0(6)).ToString()
)

For #e66465, the result is:

230,100,101

How the conversion works

Each two-character component is calculated as firstDigit * 16 + secondDigit.

For the red component e6:

14 * 16 + 6 = 230

The packed decimal expression then places red, green, and blue into successive 256-value ranges:

red * 256 * 256 + green * 256 + blue

Requirements and limitations

  • These expressions expect a full #RRGGBB value. Do not use a three-digit shorthand value such as #f00 without converting it to #ff0000 first.
  • The lookup string contains the lowercase hexadecimal digits 1 through f. The tolower call handles uppercase letters in the input.
  • Use the packed-number expression only when the receiving context expects one numeric RGB value. Use the RGB-string expression when the receiving context expects three comma-separated decimal components.
  • OCL is a query language and expressions must not change model data. Use OCL for derived values, constraints, and other side-effect-free expressions. For changes to data, use EAL where appropriate.

See also