|
|
|
|
- Overview
- Declaration
- Example
- Replace tabs with spaces
- Extension of the Color type
By using extension procedures, you can add your own WLanguage procedures to different types of variables (string, date, color etc.). These procedures can then be called as native WLanguage functions. The basic WLanguage type is therefore "extended". To indicate that a procedure is an extension procedure, the following conditions must be met: - the first parameter uses the type that must be "extended",
- the prototype of the procedure has the <extension> attribute.
New in version 28Declaration of an extension procedure of the String type: PROCEDURE MyCustomProcedure(s is string) <extension> Remarks: - The extension procedures appear in the code completion suggestions of the extended type.
- Extension procedures cannot be used with types such as Boolean, Integer, Real, Numeric, etc. (any type used to handle numeric values).
- Only WLanguage types can be extended.
- Only global procedures can have the <extension> attribute.
- If the extension procedure overrides a WLanguage function, the standard syntax of that function will also be overridden. For example, creating a "Format" extension procedure for the String type will override both the <String>.Format and StringFormat functions.
- You can chain the calls to extension procedures.
Replace tabs with spaces Procedure to replace tabs with spaces: PROCEDURE ReplaceTABWithXSpace(s is string, n is int) <extension>: string RETURN s.Replace(TAB, RepeatString(" ", n)) Call to the extension procedure: sText is string = TAB + "XXX" Trace(sText) // <TAB>XXX Trace(sText.ReplaceTABWithXSpace(4)) // XXX Extension of the Color type Extension procedure of the Color type, used to get a readable text color on a given background color. PROCEDURE ReadableTextColor(SourceColor is Color) <extension>: Color nLightness is int rLuminance is real ColorResult is Color // Calculate the luminance of the color passed as parameter rLuminance = 1 - (0.299 * SourceColor..Red + ... 0.587 * SourceColor..Green + 0.114 * SourceColor..Blue)/255 // According to the luminance, the text color must have a low or high lightness IF rLuminance < 0.5 THEN // Low luminosity nLightness = 12 ELSE // High luminosity nLightness = 88 END // The returned color keeps the shade and the saturation ColorResult = HSL(ColorHue(SourceColor), ... ColorSaturation(SourceColor), nLightness) RETURN ColorResult Call to the extension procedure: // Array of colors arrHTMLColors is array of strings arrHTMLColors.Add("#F48FB1") arrHTMLColors.Add("#C2185B") arrHTMLColors.Add("#64B5F6") arrHTMLColors.Add("#1565C0") arrHTMLColors.Add("#81C784") arrHTMLColors.Add("#2E7D32") MyColor is Color // Gets a random color MyColor = HTMLToRGB(arrHTMLColors[Random(1, arrHTMLColors..Count)] ) // Sets the initial color as background color STC_BackgroundColorTest.BackgroundColor = MyColor // Sets an easily readable color as text color STC_BackgroundColorTest.Color = MyColor.ReadableTextColor()
Related Examples:
|
Training (WINDEV): ExtensionProcedures
[ + ] This example illustrates the use of the <extension> attribute, to add functionalities to WLanguage types.
|
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|