SkScalar.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. * Copyright 2010 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkMath.h"
  8. #include "include/core/SkScalar.h"
  9. SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
  10. const SkScalar values[], int length) {
  11. SkASSERT(length > 0);
  12. SkASSERT(keys != nullptr);
  13. SkASSERT(values != nullptr);
  14. #ifdef SK_DEBUG
  15. for (int i = 1; i < length; i++)
  16. SkASSERT(keys[i] >= keys[i-1]);
  17. #endif
  18. int right = 0;
  19. while (right < length && searchKey > keys[right])
  20. right++;
  21. // Could use sentinel values to eliminate conditionals, but since the
  22. // tables are taken as input, a simpler format is better.
  23. if (length == right)
  24. return values[length-1];
  25. if (0 == right)
  26. return values[0];
  27. // Otherwise, interpolate between right - 1 and right.
  28. SkScalar rightKey = keys[right];
  29. SkScalar leftKey = keys[right-1];
  30. SkScalar fract = (searchKey - leftKey) / (rightKey - leftKey);
  31. return SkScalarInterp(values[right-1], values[right], fract);
  32. }