skia_utils_mac.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SKIA_EXT_SKIA_UTILS_MAC_H_
  5. #define SKIA_EXT_SKIA_UTILS_MAC_H_
  6. #include <ApplicationServices/ApplicationServices.h>
  7. #include "base/mac/scoped_cftyperef.h"
  8. #include "third_party/skia/include/core/SkBitmap.h"
  9. #include "third_party/skia/include/core/SkColor.h"
  10. #include "third_party/skia/include/core/SkPixmap.h"
  11. struct SkIRect;
  12. struct SkRect;
  13. class SkMatrix;
  14. using NSSize = CGSize;
  15. #ifdef __OBJC__
  16. @class NSBitmapImageRep;
  17. @class NSImage;
  18. @class NSImageRep;
  19. @class NSColor;
  20. #else
  21. class NSBitmapImageRep;
  22. class NSImage;
  23. class NSImageRep;
  24. class NSColor;
  25. #endif
  26. namespace skia {
  27. // Matrix converters.
  28. SK_API CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix);
  29. // Rectangle converters.
  30. SK_API SkRect CGRectToSkRect(const CGRect& rect);
  31. // Converts a Skia rect to a CoreGraphics CGRect.
  32. CGRect SkIRectToCGRect(const SkIRect& rect);
  33. CGRect SkRectToCGRect(const SkRect& rect);
  34. // Converts NSColor to an SKColor.
  35. // NSColor has a number of methods that return system colors (i.e. controlled by
  36. // user preferences). This function converts the color given by an NSColor class
  37. // method to an SkColor. Official documentation suggests developers only rely on
  38. // +[NSColor selectedTextBackgroundColor] and +[NSColor selectedControlColor],
  39. // but other colors give a good baseline. For many, a gradient is involved; the
  40. // palette chosen based on the enum value given by +[NSColor currentColorTint].
  41. // Apple's documentation also suggests to use NSColorList, but the system color
  42. // list is just populated with class methods on NSColor.
  43. SK_API SkColor NSSystemColorToSkColor(NSColor* color);
  44. // Converts CGColorRef to the ARGB layout Skia expects. The given CGColorRef
  45. // should be in the sRGB color space and include alpha.
  46. SK_API SkColor CGColorRefToSkColor(CGColorRef color);
  47. // Converts a Skia ARGB color to CGColorRef. Assumes sRGB color space.
  48. SK_API base::ScopedCFTypeRef<CGColorRef> CGColorCreateFromSkColor(
  49. SkColor color);
  50. // Converts NSColor to ARGB. Returns raw rgb values and does no colorspace
  51. // conversion. Only valid for colors in calibrated and device color spaces.
  52. SK_API SkColor NSDeviceColorToSkColor(NSColor* color);
  53. // Converts ARGB in the specified color space to NSColor.
  54. // Prefer sRGB over calibrated colors.
  55. SK_API NSColor* SkColorToCalibratedNSColor(SkColor color);
  56. SK_API NSColor* SkColorToDeviceNSColor(SkColor color);
  57. SK_API NSColor* SkColorToSRGBNSColor(SkColor color);
  58. // Converts a CGImage to a SkBitmap.
  59. SK_API SkBitmap CGImageToSkBitmap(CGImageRef image);
  60. // Draws an NSImage with a given size into a SkBitmap.
  61. SK_API SkBitmap NSImageToSkBitmapWithColorSpace(NSImage* image,
  62. bool is_opaque,
  63. CGColorSpaceRef color_space);
  64. // Draws an NSImageRep with a given size into a SkBitmap.
  65. SK_API SkBitmap NSImageRepToSkBitmapWithColorSpace(NSImageRep* image,
  66. NSSize size,
  67. bool is_opaque,
  68. CGColorSpaceRef colorspace);
  69. // Given an SkBitmap, return an autoreleased NSBitmapImageRep.
  70. SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRepWithColorSpace(
  71. const SkBitmap& skiaBitmap,
  72. CGColorSpaceRef colorSpace);
  73. // Given an SkBitmap and a color space, return an autoreleased NSImage.
  74. SK_API NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon,
  75. CGColorSpaceRef colorSpace);
  76. } // namespace skia
  77. #endif // SKIA_EXT_SKIA_UTILS_MAC_H_