Utils.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (C) 2016 Jared Rummler
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package com.jaredrummler.materialspinner;
  18. import android.content.Context;
  19. import android.graphics.Color;
  20. import android.graphics.drawable.Drawable;
  21. import android.os.Build;
  22. import android.view.View;
  23. final class Utils {
  24. /**
  25. * Darkens a color by a given factor.
  26. *
  27. * @param color the color to darken
  28. * @param factor The factor to darken the color.
  29. * @return darker version of specified color.
  30. */
  31. static int darker(int color, float factor) {
  32. return Color.argb(Color.alpha(color), Math.max((int) (Color.red(color) * factor), 0),
  33. Math.max((int) (Color.green(color) * factor), 0), Math.max((int) (Color.blue(color) * factor), 0));
  34. }
  35. /**
  36. * Lightens a color by a given factor.
  37. *
  38. * @param color The color to lighten
  39. * @param factor The factor to lighten the color. 0 will make the color unchanged. 1 will make the
  40. * color white.
  41. * @return lighter version of the specified color.
  42. */
  43. static int lighter(int color, float factor) {
  44. int red = (int) ((Color.red(color) * (1 - factor) / 255 + factor) * 255);
  45. int green = (int) ((Color.green(color) * (1 - factor) / 255 + factor) * 255);
  46. int blue = (int) ((Color.blue(color) * (1 - factor) / 255 + factor) * 255);
  47. return Color.argb(Color.alpha(color), red, green, blue);
  48. }
  49. /**
  50. * Check if layout direction is RTL
  51. *
  52. * @param context the current context
  53. * @return {@code true} if the layout direction is right-to-left
  54. */
  55. static boolean isRtl(Context context) {
  56. return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
  57. && context.getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
  58. }
  59. /**
  60. * Return a drawable object associated with a particular resource ID.
  61. *
  62. * <p>Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned drawable will be styled for the
  63. * specified Context's theme.</p>
  64. *
  65. * @param id The desired resource identifier, as generated by the aapt tool.
  66. * This integer encodes the package, type, and resource entry.
  67. * The value 0 is an invalid identifier.
  68. * @return Drawable An object that can be used to draw this resource.
  69. */
  70. static Drawable getDrawable(Context context, int id) {
  71. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  72. return context.getDrawable(id);
  73. }
  74. return context.getResources().getDrawable(id);
  75. }
  76. }