ContrastDisplayContext.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // ContrastDisplayContext.swift
  3. // DynamicColorExample
  4. //
  5. // Created by Yannick LORIOT on 26/11/2016.
  6. // Copyright © 2016 Yannick LORIOT. All rights reserved.
  7. //
  8. import Foundation
  9. #if os(iOS) || os(tvOS) || os(watchOS)
  10. import UIKit
  11. #elseif os(OSX)
  12. import AppKit
  13. #endif
  14. extension DynamicColor {
  15. /**
  16. Used to describe the context of display of 2 colors.
  17. Based on WCAG: https://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast-contrast
  18. */
  19. public enum ContrastDisplayContext {
  20. /**
  21. A standard text in a normal context.
  22. */
  23. case standard
  24. /**
  25. A large text in a normal context.
  26. You can look here for the definition of "large text":
  27. https://www.w3.org/TR/2008/REC-WCAG20-20081211/#larger-scaledef
  28. */
  29. case standardLargeText
  30. /**
  31. A standard text in an enhanced context.
  32. Enhanced means that you want to be accessible (and AAA compliant in WCAG)
  33. */
  34. case enhanced
  35. /**
  36. A large text in an enhanced context.
  37. Enhanced means that you want to be accessible (and AAA compliant in WCAG)
  38. You can look here for the definition of "large text":
  39. https://www.w3.org/TR/2008/REC-WCAG20-20081211/#larger-scaledef
  40. */
  41. case enhancedLargeText
  42. var minimumContrastRatio: CGFloat {
  43. switch self {
  44. case .standard:
  45. return 4.5
  46. case .standardLargeText:
  47. return 3.0
  48. case .enhanced:
  49. return 7.0
  50. case .enhancedLargeText:
  51. return 4.5
  52. }
  53. }
  54. }
  55. }