applySkinTone method

Emoji applySkinTone(
  1. Emoji emoji,
  2. String color
)

Applies skin tone to given emoji

Any existing skin tone modifier is stripped first, so re-applying a tone to an already toned glyph produces a valid single-modifier sequence instead of an invalid double-modifier one (e.g. 👋🏻🏽).

Implementation

Emoji applySkinTone(Emoji emoji, String color) {
  final runes = removeSkinTone(emoji).emoji.runes.toList();
  if (runes.isEmpty) {
    return emoji;
  }
  // The tone modifier has to immediately follow the base code point, so the
  // base is one rune - not two UTF-16 code units, which would also swallow a
  // variation selector for bases outside the BMP. The modifier implies emoji
  // presentation, so a selector right after the base is dropped rather than
  // carried along (✌️ + 🏽 is 270C 1F3FD, not 270C FE0F 1F3FD).
  final restIndex = runes.length > 1 && runes[1] == 0xFE0F ? 2 : 1;
  final result = <int>[
    // Base emoji without gender, presentation selector, etc.
    runes.first,
    // Skin tone
    ...color.runes,
    // The rest of the emoji (gender, etc.) again
    ...runes.skip(restIndex),
  ];
  return emoji.copyWith(emoji: String.fromCharCodes(result));
}