I'm writing a test application to understand the new UDF features like parameters and return types and while things are better there's certainly some room for improvement, or maybe I just don't know Untyped Objects? I wrote the below code to take a color value in HSL and convert it into an RGB color which is fine if I just want to return a Color for use in a Fill property etc. but I would definitely prefer to return the R, G and B values separately. Is there any way to do this? I can't find much documentation on the changes to UDF, I presume the variables are passed to the function by value and not by reference. Maybe there's some way of returning 3 values as an Untyped Object that I could then call Set(uo, hsl2RGB(230, 0.46, 0.22)); Then cast back to a number like Set(Red, Value(uo.R))?
Chroma(L:Number, S:Number):Number = (1.0 - Abs(2.0 * L - 1.0)) * S;
XComp(C:Number, H:Number):Number = C * (1.0 - Abs(Mod(H / 60.0, 2.0) - 1.0));
ML(L:Number, C:Number):Number = L - (C / 2);
hsl2RGB(H:Number, S:Number, L:Number):Color =
With( {chroma: Chroma(L, S)},
With({c: (chroma + ML(L, chroma)) * 255,
x: (XComp(chroma, H) + ML(L, chroma)) * 255 },
Switch(RoundUp(H / 60.0, 0),
1, RGBA( c, x, ML(L, chroma) * 255, 1 ),
2, RGBA( x, c, ML(L, chroma) * 255, 1 ),
3, RGBA( ML(L, chroma) * 255, c, x, 1 ),
4, RGBA( ML(L, chroma) * 255, x, c, 1 ),
5, RGBA( x, ML(L, chroma) * 255, c, 1 ),
RGBA( c, ML(L, chroma) * 255, x, 1)
)
)
);
Can confirm this works, and works well, great job! The only annoyance is that autocomplete/intellisense is unaware of the object's fields (no surprises there).
Hello! I was trying to achieve the same thing but couldn't get it to accept the normal record notation. I found this workaround:
// get the individual RGBA number values from a Color object; returning an object with the R, G, B, and A values
RGBAfromColor(color:Color):UntypedObject =
ParseJSON(
JSON(
{
R: Text(Hex2Dec(Mid(JSON(color), 3, 2))),
G: Text(Hex2Dec(Mid(JSON(color), 5, 2))),
B: Text(Hex2Dec(Mid(JSON(color), 7, 2))),
A: Text(Hex2Dec(Mid(JSON(color), 9, 2)))
}
)
)
;
The JSON provides the string representation of the object/record to ParseJSON which creates the returned UntypedObject. I'm not sure why this is necessary, and there may be a more elegant solution, but it has been working!
Here is an example of using the returned UntypedObject:
// apply a tint to a color given the base Color object, the tint Color object, and the opacity of the tint (using alpha blending); returning a Color object
BlendColorWithTint(BaseColor:Color, TintColor:Color, TintOpacity:Number):Color =
With(
{
Base: RGBAfromColor(BaseColor),
Tint: RGBAfromColor(TintColor)
},
RGBA(
RoundDown((1 - TintOpacity) * Base.R + TintOpacity * Tint.R, 0),
RoundDown((1 - TintOpacity) * Base.G + TintOpacity * Tint.G, 0),
RoundDown((1 - TintOpacity) * Base.B + TintOpacity * Tint.B, 0),
1
)
)
;
(And apologies if I am not using terminology correctly, I am relatively new to Power Apps).
Hi, I've started experimenting with UDF as well, and have been a little frustrated at certain formats not being supported, but as it's an experimental feature, I'm sure that support for Tables and Records will be coming - it would expand the UDF possibilities massively.
So... All UDF's will output a 'result' based on your inputs, and the output will always be in the same format. Could you wrap the UDF in another Split, Len, Right, Left etc sort of formula that will break the result into the individual parts that you want?? Just a 'top of my head idea, hehe.....
--
Ian Sanders.
CEO, The Real Brits,
Dubai, UAE
--
Stay up to date on forum activity by subscribing.
mmbr1606
49
Super User 2025 Season 1
MS.Ragavendar
22
stanmiller95
16