Clamp rotations when passing zero- maths

This is more of a maths question that OF, but maybe someone here can help, I am trying to limit the rotation of an image when it is linked to input from sensors.

I get my rotation input as a value between 0 and 360 in degrees, I have to adjust its latent or inactive position by an offset and limit its clockwise and anticlockwise rotation (preferably to scale the full range of rotation to within my upper and lower limits).

The problem I get is when the rotation crosses the zero point as I am using the upper and lower limits to map/limit the values.

When the value crosses zero my upper and lower limits are no longer usable as they are flipped. Eg

Incoming rotation is 10, my upper limit is 25 and lower is -5, but the -5 is actually 355 and I then need to map the values from 355 - 360 (my -5 lower limit) and then from 0 to 10- (so my range from minimum to centre location is smallest 355 and largest 10) where it becomes equal to the latent rotation. Here I need to switch to using my upper limit scale to map the forward rotation to within my range.

My previous effort led to this

ofRotateZ(ofClamp(ofAngleDifferenceDegrees(ofWrapDegrees(incomingRotation), ofWrapDegrees(latentFixedRotation)), rotationMinLimit, rotationMaxLimit));

Obviously this does not work, is there a way to deal with this type of scenario- I can remap the values between -180 and +180 but as I am dealing with a number of instances and the conditions will change I am really looking for a way around this.