I have this code:
type
ColorName = public record
private
colorValue: UInt32;
protected
public
constructor( colorInt : UInt32 );
constructor( color : Color );
constructor( colorString : String );
class operator Implicit( color : ColorName ) : UInt32;
class operator Implicit( color : ColorName ) : Brush;
class operator Implicit( color : ColorName ) : SolidColorBrush;
class operator Implicit( color : ColorName ) : Color;
class operator Implicit( color : UInt32 ) : ColorName;
class function ColorValueFromColor( color : Color ) : UInt32;
...
class Transparent : ColorName := $00FFFFFF; readonly; <============ error
class YellowGreen : ColorName := $FF9ACD32; readonly;
end;
implementation
class operator ColorName.Implicit( color : ColorName ) : Uint32;
begin
result := color.ColorValue;
end;
class operator ColorName.Implicit( color : ColorName ) : Brush;
begin
result := new SolidColorBrush( color );
end;
class operator ColorName.Implicit( color : ColorName ) : Color;
begin
var value: UInt32 := color;
result := System.Windows.Media.Color.FromArgb(
( value shr 24 ) as Byte,
( value shr 16 ) as Byte,
( value shr 8 ) as Byte,
value as Byte);
end;
class operator ColorName.Implicit( color : UInt32 ) : ColorName;
begin
result := new ColorName( color );
end;
constructor ColorName( colorInt : UInt32 );
begin
ColorValue := colorInt;
end;
constructor ColorName( color: Color );
begin
ColorValue := ColorValueFromColor( color );
end;
class operator ColorName.Implicit( color : ColorName ) : SolidColorBrush;
begin
result := new SolidColorBrush( color );
end;
constructor ColorName( colorString : String );
var
alpha : Byte;
r : Byte;
g : Byte;
b : Byte;
function IsPredefinedColor : Boolean;
begin
result := True;
if colorString.Equals( 'AliceBlue' ) then colorValue := AliceBlue
else if colorString.Equals( 'Transparent' ) then colorValue := Transparent
else result := False;
end;
begin
// takes a string like '$FFFA8072' which is Alpha=FF R=FA G=80 B=72
// or '#FFFA8072'
// or 'Salmon'
// and converts it into an integer ColorValue
// first char may or may not be $ or #
if ( colorString[0] = '$' ) or
( colorString[0] = '#' ) then begin
alpha := Convert.ToByte( colorString.Substring( 1,2 ), 16 );
r := Convert.ToByte( colorString.Substring( 3,2 ), 16 );
g := Convert.ToByte( colorString.Substring( 5,2 ), 16 );
b := Convert.ToByte( colorString.Substring( 7,2 ), 16 );
end
else if IsPredefinedColor then exit
else begin
alpha := $FF; // no alpha so assume this value
r := Convert.ToByte( colorString.Substring( 0,2 ), 16 );
g := Convert.ToByte( colorString.Substring( 2,2 ), 16 );
b := Convert.ToByte( colorString.Substring( 4,2 ), 16 );
end;
colorValue := ColorValueFromColor( System.Windows.Media.Color.FromArgb( alpha, r, g, b ) );
end;
class function ColorName.ColorValueFromColor( color : Color ) : UInt32;
begin
result := ( Color.A shl 24 )+
( Color.R shl 16 ) +
( Color.G shl 8 ) +
( Color.B );
end;
end.
I chopped out some of the color constants, but this used to compile quite a while ago, but doesn’t now.
The error is ‘error E62: Type mismatch, cannot assign “Int32” to “ColorName”’
I suspect the compiler is chopping off the leading 00. If I change it to FF, it compiles fine.
If I CAST it like:
class Transparent : ColorName := uint32( $00FFFFFF); readonly;
Then it compiles.