static inline CGFloat CKRoundValueToPixelGrid()

in RenderCore/Utilities/CKInternalHelpers.h [43:65]


static inline CGFloat CKRoundValueToPixelGrid(CGFloat value, const BOOL forceCeil,
                                              const BOOL forceFloor) noexcept
{
    CGFloat scale = CKScreenScale();
    CGFloat scaledValue = value * scale;
    CGFloat fractial = fmodf(scaledValue, 1.0);
    if (CKFloatsEqual(fractial, 0)) {
        // First we check if the value is already rounded
        scaledValue = scaledValue - fractial;
    } else if (CKFloatsEqual(fractial, 1.0)) {
        scaledValue = scaledValue - fractial + 1.0;
    } else if (forceCeil) {
        // Next we check if we need to use forced rounding
        scaledValue = scaledValue - fractial + 1.0f;
    } else if (forceFloor) {
        scaledValue = scaledValue - fractial;
    } else {
        // Finally we just round the value
        scaledValue = scaledValue - fractial +
        (fractial > 0.5f || CKFloatsEqual(fractial, 0.5f) ? 1.0f : 0.0f);
    }
    return scaledValue / scale;
}